Mercurial > vim
annotate src/misc1.c @ 6112:d594c1552137 v7.4.394
updated for version 7.4.394
Problem: When using DirectX last italic character is incomplete.
Solution: Add one to the number of cells. (Ken Takata)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 06 Aug 2014 16:49:55 +0200 |
parents | d3a674f6c737 |
children | 65b0974c5beb |
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)); | |
4940
8bbd0ff486b3
updated for version 7.3.1215
Bram Moolenaar <bram@vim.org>
parents:
4938
diff
changeset
|
19 #if defined(FEAT_CMDL_COMPL) |
4938
bcb84438bb5b
updated for version 7.3.1214
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
20 static void init_users __ARGS((void)); |
4940
8bbd0ff486b3
updated for version 7.3.1215
Bram Moolenaar <bram@vim.org>
parents:
4938
diff
changeset
|
21 #endif |
7 | 22 static int copy_indent __ARGS((int size, char_u *src)); |
23 | |
3744 | 24 /* All user names (for ~user completion as done by shell). */ |
25 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
26 static garray_T ga_users; | |
27 #endif | |
28 | |
7 | 29 /* |
30 * Count the size (in window cells) of the indent in the current line. | |
31 */ | |
32 int | |
33 get_indent() | |
34 { | |
5995 | 35 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE); |
7 | 36 } |
37 | |
38 /* | |
39 * Count the size (in window cells) of the indent in line "lnum". | |
40 */ | |
41 int | |
42 get_indent_lnum(lnum) | |
43 linenr_T lnum; | |
44 { | |
5995 | 45 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE); |
7 | 46 } |
47 | |
48 #if defined(FEAT_FOLDING) || defined(PROTO) | |
49 /* | |
50 * Count the size (in window cells) of the indent in line "lnum" of buffer | |
51 * "buf". | |
52 */ | |
53 int | |
54 get_indent_buf(buf, lnum) | |
55 buf_T *buf; | |
56 linenr_T lnum; | |
57 { | |
5995 | 58 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE); |
7 | 59 } |
60 #endif | |
61 | |
62 /* | |
63 * count the size (in window cells) of the indent in line "ptr", with | |
64 * 'tabstop' at "ts" | |
65 */ | |
164 | 66 int |
5995 | 67 get_indent_str(ptr, ts, list) |
7 | 68 char_u *ptr; |
69 int ts; | |
5995 | 70 int list; /* if TRUE, count only screen size for tabs */ |
7 | 71 { |
72 int count = 0; | |
73 | |
74 for ( ; *ptr; ++ptr) | |
75 { | |
5995 | 76 if (*ptr == TAB) |
77 { | |
78 if (!list || lcs_tab1) /* count a tab for what it is worth */ | |
79 count += ts - (count % ts); | |
80 else | |
81 /* in list mode, when tab is not set, count screen char width for Tab: ^I */ | |
82 count += ptr2cells(ptr); | |
83 } | |
7 | 84 else if (*ptr == ' ') |
85 ++count; /* count a space for one */ | |
86 else | |
87 break; | |
88 } | |
164 | 89 return count; |
7 | 90 } |
91 | |
92 /* | |
93 * Set the indent of the current line. | |
94 * Leaves the cursor on the first non-blank in the line. | |
95 * Caller must take care of undo. | |
96 * "flags": | |
97 * SIN_CHANGED: call changed_bytes() if the line was changed. | |
98 * SIN_INSERT: insert the indent in front of the line. | |
99 * SIN_UNDO: save line for undo before changing it. | |
100 * Returns TRUE if the line was changed. | |
101 */ | |
102 int | |
103 set_indent(size, flags) | |
1324 | 104 int size; /* measured in spaces */ |
7 | 105 int flags; |
106 { | |
107 char_u *p; | |
108 char_u *newline; | |
109 char_u *oldline; | |
110 char_u *s; | |
111 int todo; | |
1324 | 112 int ind_len; /* measured in characters */ |
7 | 113 int line_len; |
114 int doit = FALSE; | |
1324 | 115 int ind_done = 0; /* measured in spaces */ |
7 | 116 int tab_pad; |
217 | 117 int retval = FALSE; |
1359 | 118 int orig_char_len = -1; /* number of initial whitespace chars when |
1324 | 119 'et' and 'pi' are both set */ |
7 | 120 |
121 /* | |
122 * First check if there is anything to do and compute the number of | |
123 * characters needed for the indent. | |
124 */ | |
125 todo = size; | |
126 ind_len = 0; | |
127 p = oldline = ml_get_curline(); | |
128 | |
129 /* Calculate the buffer size for the new indent, and check to see if it | |
130 * isn't already set */ | |
131 | |
1324 | 132 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
133 * 'preserveindent' are set count the number of characters at the | |
134 * beginning of the line to be copied */ | |
135 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) | |
7 | 136 { |
137 /* If 'preserveindent' is set then reuse as much as possible of | |
138 * the existing indent structure for the new indent */ | |
139 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
140 { | |
141 ind_done = 0; | |
142 | |
143 /* count as many characters as we can use */ | |
144 while (todo > 0 && vim_iswhite(*p)) | |
145 { | |
146 if (*p == TAB) | |
147 { | |
148 tab_pad = (int)curbuf->b_p_ts | |
149 - (ind_done % (int)curbuf->b_p_ts); | |
150 /* stop if this tab will overshoot the target */ | |
151 if (todo < tab_pad) | |
152 break; | |
153 todo -= tab_pad; | |
154 ++ind_len; | |
155 ind_done += tab_pad; | |
156 } | |
157 else | |
158 { | |
159 --todo; | |
160 ++ind_len; | |
161 ++ind_done; | |
162 } | |
163 ++p; | |
164 } | |
165 | |
1324 | 166 /* Set initial number of whitespace chars to copy if we are |
167 * preserving indent but expandtab is set */ | |
168 if (curbuf->b_p_et) | |
169 orig_char_len = ind_len; | |
170 | |
7 | 171 /* Fill to next tabstop with a tab, if possible */ |
172 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
1359 | 173 if (todo >= tab_pad && orig_char_len == -1) |
7 | 174 { |
175 doit = TRUE; | |
176 todo -= tab_pad; | |
177 ++ind_len; | |
178 /* ind_done += tab_pad; */ | |
179 } | |
180 } | |
181 | |
182 /* count tabs required for indent */ | |
183 while (todo >= (int)curbuf->b_p_ts) | |
184 { | |
185 if (*p != TAB) | |
186 doit = TRUE; | |
187 else | |
188 ++p; | |
189 todo -= (int)curbuf->b_p_ts; | |
190 ++ind_len; | |
191 /* ind_done += (int)curbuf->b_p_ts; */ | |
192 } | |
193 } | |
194 /* count spaces required for indent */ | |
195 while (todo > 0) | |
196 { | |
197 if (*p != ' ') | |
198 doit = TRUE; | |
199 else | |
200 ++p; | |
201 --todo; | |
202 ++ind_len; | |
203 /* ++ind_done; */ | |
204 } | |
205 | |
206 /* Return if the indent is OK already. */ | |
207 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT)) | |
208 return FALSE; | |
209 | |
210 /* Allocate memory for the new line. */ | |
211 if (flags & SIN_INSERT) | |
212 p = oldline; | |
213 else | |
214 p = skipwhite(p); | |
215 line_len = (int)STRLEN(p) + 1; | |
1324 | 216 |
217 /* If 'preserveindent' and 'expandtab' are both set keep the original | |
218 * characters and allocate accordingly. We will fill the rest with spaces | |
219 * after the if (!curbuf->b_p_et) below. */ | |
1359 | 220 if (orig_char_len != -1) |
1324 | 221 { |
222 newline = alloc(orig_char_len + size - ind_done + line_len); | |
223 if (newline == NULL) | |
224 return FALSE; | |
1359 | 225 todo = size - ind_done; |
226 ind_len = orig_char_len + todo; /* Set total length of indent in | |
227 * characters, which may have been | |
228 * undercounted until now */ | |
1324 | 229 p = oldline; |
230 s = newline; | |
231 while (orig_char_len > 0) | |
232 { | |
233 *s++ = *p++; | |
234 orig_char_len--; | |
235 } | |
1474 | 236 |
1324 | 237 /* Skip over any additional white space (useful when newindent is less |
238 * than old) */ | |
239 while (vim_iswhite(*p)) | |
1474 | 240 ++p; |
1348 | 241 |
1324 | 242 } |
243 else | |
244 { | |
245 todo = size; | |
246 newline = alloc(ind_len + line_len); | |
247 if (newline == NULL) | |
248 return FALSE; | |
249 s = newline; | |
250 } | |
7 | 251 |
252 /* Put the characters in the new line. */ | |
253 /* if 'expandtab' isn't set: use TABs */ | |
254 if (!curbuf->b_p_et) | |
255 { | |
256 /* If 'preserveindent' is set then reuse as much as possible of | |
257 * the existing indent structure for the new indent */ | |
258 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
259 { | |
260 p = oldline; | |
261 ind_done = 0; | |
262 | |
263 while (todo > 0 && vim_iswhite(*p)) | |
264 { | |
265 if (*p == TAB) | |
266 { | |
267 tab_pad = (int)curbuf->b_p_ts | |
268 - (ind_done % (int)curbuf->b_p_ts); | |
269 /* stop if this tab will overshoot the target */ | |
270 if (todo < tab_pad) | |
271 break; | |
272 todo -= tab_pad; | |
273 ind_done += tab_pad; | |
274 } | |
275 else | |
276 { | |
277 --todo; | |
278 ++ind_done; | |
279 } | |
280 *s++ = *p++; | |
281 } | |
282 | |
283 /* Fill to next tabstop with a tab, if possible */ | |
284 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
285 if (todo >= tab_pad) | |
286 { | |
287 *s++ = TAB; | |
288 todo -= tab_pad; | |
289 } | |
290 | |
291 p = skipwhite(p); | |
292 } | |
293 | |
294 while (todo >= (int)curbuf->b_p_ts) | |
295 { | |
296 *s++ = TAB; | |
297 todo -= (int)curbuf->b_p_ts; | |
298 } | |
299 } | |
300 while (todo > 0) | |
301 { | |
302 *s++ = ' '; | |
303 --todo; | |
304 } | |
305 mch_memmove(s, p, (size_t)line_len); | |
306 | |
307 /* Replace the line (unless undo fails). */ | |
308 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) | |
309 { | |
310 ml_replace(curwin->w_cursor.lnum, newline, FALSE); | |
311 if (flags & SIN_CHANGED) | |
312 changed_bytes(curwin->w_cursor.lnum, 0); | |
5403 | 313 /* Correct saved cursor position if it is in this line. */ |
314 if (saved_cursor.lnum == curwin->w_cursor.lnum) | |
315 { | |
316 if (saved_cursor.col >= (colnr_T)(p - oldline)) | |
317 /* cursor was after the indent, adjust for the number of | |
318 * bytes added/removed */ | |
319 saved_cursor.col += ind_len - (colnr_T)(p - oldline); | |
320 else if (saved_cursor.col >= (colnr_T)(s - newline)) | |
321 /* cursor was in the indent, and is now after it, put it back | |
322 * at the start of the indent (replacing spaces with TAB) */ | |
323 saved_cursor.col = (colnr_T)(s - newline); | |
324 } | |
217 | 325 retval = TRUE; |
7 | 326 } |
327 else | |
328 vim_free(newline); | |
329 | |
330 curwin->w_cursor.col = ind_len; | |
217 | 331 return retval; |
7 | 332 } |
333 | |
334 /* | |
335 * Copy the indent from ptr to the current line (and fill to size) | |
336 * Leaves the cursor on the first non-blank in the line. | |
337 * Returns TRUE if the line was changed. | |
338 */ | |
339 static int | |
340 copy_indent(size, src) | |
341 int size; | |
342 char_u *src; | |
343 { | |
344 char_u *p = NULL; | |
345 char_u *line = NULL; | |
346 char_u *s; | |
347 int todo; | |
348 int ind_len; | |
349 int line_len = 0; | |
350 int tab_pad; | |
351 int ind_done; | |
352 int round; | |
353 | |
354 /* Round 1: compute the number of characters needed for the indent | |
355 * Round 2: copy the characters. */ | |
356 for (round = 1; round <= 2; ++round) | |
357 { | |
358 todo = size; | |
359 ind_len = 0; | |
360 ind_done = 0; | |
361 s = src; | |
362 | |
363 /* Count/copy the usable portion of the source line */ | |
364 while (todo > 0 && vim_iswhite(*s)) | |
365 { | |
366 if (*s == TAB) | |
367 { | |
368 tab_pad = (int)curbuf->b_p_ts | |
369 - (ind_done % (int)curbuf->b_p_ts); | |
370 /* Stop if this tab will overshoot the target */ | |
371 if (todo < tab_pad) | |
372 break; | |
373 todo -= tab_pad; | |
374 ind_done += tab_pad; | |
375 } | |
376 else | |
377 { | |
378 --todo; | |
379 ++ind_done; | |
380 } | |
381 ++ind_len; | |
840 | 382 if (p != NULL) |
7 | 383 *p++ = *s; |
384 ++s; | |
385 } | |
386 | |
387 /* Fill to next tabstop with a tab, if possible */ | |
388 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
3058 | 389 if (todo >= tab_pad && !curbuf->b_p_et) |
7 | 390 { |
391 todo -= tab_pad; | |
392 ++ind_len; | |
840 | 393 if (p != NULL) |
7 | 394 *p++ = TAB; |
395 } | |
396 | |
397 /* Add tabs required for indent */ | |
3058 | 398 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et) |
7 | 399 { |
400 todo -= (int)curbuf->b_p_ts; | |
401 ++ind_len; | |
840 | 402 if (p != NULL) |
7 | 403 *p++ = TAB; |
404 } | |
405 | |
406 /* Count/add spaces required for indent */ | |
407 while (todo > 0) | |
408 { | |
409 --todo; | |
410 ++ind_len; | |
840 | 411 if (p != NULL) |
7 | 412 *p++ = ' '; |
413 } | |
414 | |
840 | 415 if (p == NULL) |
7 | 416 { |
417 /* Allocate memory for the result: the copied indent, new indent | |
418 * and the rest of the line. */ | |
419 line_len = (int)STRLEN(ml_get_curline()) + 1; | |
420 line = alloc(ind_len + line_len); | |
421 if (line == NULL) | |
422 return FALSE; | |
423 p = line; | |
424 } | |
425 } | |
426 | |
427 /* Append the original line */ | |
428 mch_memmove(p, ml_get_curline(), (size_t)line_len); | |
429 | |
430 /* Replace the line */ | |
431 ml_replace(curwin->w_cursor.lnum, line, FALSE); | |
432 | |
433 /* Put the cursor after the indent. */ | |
434 curwin->w_cursor.col = ind_len; | |
435 return TRUE; | |
436 } | |
437 | |
438 /* | |
439 * Return the indent of the current line after a number. Return -1 if no | |
440 * number was found. Used for 'n' in 'formatoptions': numbered list. | |
41 | 441 * Since a pattern is used it can actually handle more than numbers. |
7 | 442 */ |
443 int | |
444 get_number_indent(lnum) | |
445 linenr_T lnum; | |
446 { | |
447 colnr_T col; | |
448 pos_T pos; | |
449 | |
3632 | 450 regmatch_T regmatch; |
451 int lead_len = 0; /* length of comment leader */ | |
452 | |
7 | 453 if (lnum > curbuf->b_ml.ml_line_count) |
454 return -1; | |
41 | 455 pos.lnum = 0; |
3584 | 456 |
457 #ifdef FEAT_COMMENTS | |
3632 | 458 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */ |
459 if ((State & INSERT) || has_format_option(FO_Q_COMS)) | |
3584 | 460 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); |
3632 | 461 #endif |
462 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); | |
463 if (regmatch.regprog != NULL) | |
464 { | |
465 regmatch.rm_ic = FALSE; | |
466 | |
467 /* vim_regexec() expects a pointer to a line. This lets us | |
468 * start matching for the flp beyond any comment leader... */ | |
469 if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) | |
470 { | |
471 pos.lnum = lnum; | |
472 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); | |
41 | 473 #ifdef FEAT_VIRTUALEDIT |
3632 | 474 pos.coladd = 0; |
475 #endif | |
476 } | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
477 vim_regfree(regmatch.regprog); |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
478 } |
41 | 479 |
480 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) | |
7 | 481 return -1; |
482 getvcol(curwin, &pos, &col, NULL, NULL); | |
483 return (int)col; | |
484 } | |
485 | |
5995 | 486 #if defined(FEAT_LINEBREAK) || defined(PROTO) |
487 /* | |
488 * Return appropriate space number for breakindent, taking influencing | |
489 * parameters into account. Window must be specified, since it is not | |
490 * necessarily always the current one. | |
491 */ | |
492 int | |
493 get_breakindent_win(wp, line) | |
494 win_T *wp; | |
495 char_u *line; /* start of the line */ | |
496 { | |
497 static int prev_indent = 0; /* cached indent value */ | |
498 static long prev_ts = 0L; /* cached tabstop value */ | |
499 static char_u *prev_line = NULL; /* cached pointer to line */ | |
6010 | 500 static int prev_tick = 0; /* changedtick of cached value */ |
5995 | 501 int bri = 0; |
502 /* window width minus window margin space, i.e. what rests for text */ | |
503 const int eff_wwidth = W_WIDTH(wp) | |
504 - ((wp->w_p_nu || wp->w_p_rnu) | |
505 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL) | |
506 ? number_width(wp) + 1 : 0); | |
507 | |
508 /* used cached indent, unless pointer or 'tabstop' changed */ | |
6010 | 509 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts |
510 || prev_tick != wp->w_buffer->b_changedtick) | |
5995 | 511 { |
512 prev_line = line; | |
513 prev_ts = wp->w_buffer->b_p_ts; | |
6010 | 514 prev_tick = wp->w_buffer->b_changedtick; |
5995 | 515 prev_indent = get_indent_str(line, |
6012 | 516 (int)wp->w_buffer->b_p_ts, wp->w_p_list); |
517 } | |
518 bri = prev_indent + wp->w_p_brishift; | |
5995 | 519 |
520 /* indent minus the length of the showbreak string */ | |
521 if (wp->w_p_brisbr) | |
522 bri -= vim_strsize(p_sbr); | |
523 | |
524 /* Add offset for number column, if 'n' is in 'cpoptions' */ | |
525 bri += win_col_off2(wp); | |
526 | |
527 /* never indent past left window margin */ | |
528 if (bri < 0) | |
529 bri = 0; | |
530 /* always leave at least bri_min characters on the left, | |
531 * if text width is sufficient */ | |
532 else if (bri > eff_wwidth - wp->w_p_brimin) | |
533 bri = (eff_wwidth - wp->w_p_brimin < 0) | |
534 ? 0 : eff_wwidth - wp->w_p_brimin; | |
535 | |
536 return bri; | |
537 } | |
538 #endif | |
539 | |
540 | |
7 | 541 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) |
542 | |
543 static int cin_is_cinword __ARGS((char_u *line)); | |
544 | |
545 /* | |
546 * Return TRUE if the string "line" starts with a word from 'cinwords'. | |
547 */ | |
548 static int | |
549 cin_is_cinword(line) | |
550 char_u *line; | |
551 { | |
552 char_u *cinw; | |
553 char_u *cinw_buf; | |
554 int cinw_len; | |
555 int retval = FALSE; | |
556 int len; | |
557 | |
558 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; | |
559 cinw_buf = alloc((unsigned)cinw_len); | |
560 if (cinw_buf != NULL) | |
561 { | |
562 line = skipwhite(line); | |
563 for (cinw = curbuf->b_p_cinw; *cinw; ) | |
564 { | |
565 len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); | |
566 if (STRNCMP(line, cinw_buf, len) == 0 | |
567 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) | |
568 { | |
569 retval = TRUE; | |
570 break; | |
571 } | |
572 } | |
573 vim_free(cinw_buf); | |
574 } | |
575 return retval; | |
576 } | |
577 #endif | |
578 | |
579 /* | |
580 * open_line: Add a new line below or above the current line. | |
581 * | |
582 * For VREPLACE mode, we only add a new line when we get to the end of the | |
583 * file, otherwise we just start replacing the next line. | |
584 * | |
585 * Caller must take care of undo. Since VREPLACE may affect any number of | |
586 * lines however, it may call u_save_cursor() again when starting to change a | |
587 * new line. | |
588 * "flags": OPENLINE_DELSPACES delete spaces after cursor | |
589 * OPENLINE_DO_COM format comments | |
590 * OPENLINE_KEEPTRAIL keep trailing spaces | |
591 * OPENLINE_MARKFIX adjust mark positions after the line break | |
3584 | 592 * OPENLINE_COM_LIST format comments with list or 2nd line indent |
593 * | |
594 * "second_line_indent": indent for after ^^D in Insert mode or if flag | |
595 * OPENLINE_COM_LIST | |
7 | 596 * |
597 * Return TRUE for success, FALSE for failure | |
598 */ | |
599 int | |
3584 | 600 open_line(dir, flags, second_line_indent) |
7 | 601 int dir; /* FORWARD or BACKWARD */ |
602 int flags; | |
3584 | 603 int second_line_indent; |
7 | 604 { |
605 char_u *saved_line; /* copy of the original line */ | |
606 char_u *next_line = NULL; /* copy of the next line */ | |
607 char_u *p_extra = NULL; /* what goes to next line */ | |
608 int less_cols = 0; /* less columns for mark in new line */ | |
609 int less_cols_off = 0; /* columns to skip for mark adjust */ | |
610 pos_T old_cursor; /* old cursor position */ | |
611 int newcol = 0; /* new cursor column */ | |
612 int newindent = 0; /* auto-indent of the new line */ | |
613 int n; | |
614 int trunc_line = FALSE; /* truncate current line afterwards */ | |
615 int retval = FALSE; /* return value, default is FAIL */ | |
616 #ifdef FEAT_COMMENTS | |
617 int extra_len = 0; /* length of p_extra string */ | |
618 int lead_len; /* length of comment leader */ | |
619 char_u *lead_flags; /* position in 'comments' for comment leader */ | |
620 char_u *leader = NULL; /* copy of comment leader */ | |
621 #endif | |
622 char_u *allocated = NULL; /* allocated memory */ | |
623 #if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \ | |
624 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS) | |
625 char_u *p; | |
626 #endif | |
627 int saved_char = NUL; /* init for GCC */ | |
628 #if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS) | |
629 pos_T *pos; | |
630 #endif | |
631 #ifdef FEAT_SMARTINDENT | |
632 int do_si = (!p_paste && curbuf->b_p_si | |
633 # ifdef FEAT_CINDENT | |
634 && !curbuf->b_p_cin | |
635 # endif | |
636 ); | |
637 int no_si = FALSE; /* reset did_si afterwards */ | |
638 int first_char = NUL; /* init for GCC */ | |
639 #endif | |
640 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
641 int vreplace_mode; | |
642 #endif | |
643 int did_append; /* appended a new line */ | |
644 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ | |
645 | |
646 /* | |
647 * make a copy of the current line so we can mess with it | |
648 */ | |
649 saved_line = vim_strsave(ml_get_curline()); | |
650 if (saved_line == NULL) /* out of memory! */ | |
651 return FALSE; | |
652 | |
653 #ifdef FEAT_VREPLACE | |
654 if (State & VREPLACE_FLAG) | |
655 { | |
656 /* | |
657 * With VREPLACE we make a copy of the next line, which we will be | |
658 * starting to replace. First make the new line empty and let vim play | |
659 * with the indenting and comment leader to its heart's content. Then | |
660 * we grab what it ended up putting on the new line, put back the | |
661 * original line, and call ins_char() to put each new character onto | |
662 * the line, replacing what was there before and pushing the right | |
663 * stuff onto the replace stack. -- webb. | |
664 */ | |
665 if (curwin->w_cursor.lnum < orig_line_count) | |
666 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); | |
667 else | |
668 next_line = vim_strsave((char_u *)""); | |
669 if (next_line == NULL) /* out of memory! */ | |
670 goto theend; | |
671 | |
672 /* | |
673 * In VREPLACE mode, a NL replaces the rest of the line, and starts | |
674 * replacing the next line, so push all of the characters left on the | |
675 * line onto the replace stack. We'll push any other characters that | |
676 * might be replaced at the start of the next line (due to autoindent | |
677 * etc) a bit later. | |
678 */ | |
679 replace_push(NUL); /* Call twice because BS over NL expects it */ | |
680 replace_push(NUL); | |
681 p = saved_line + curwin->w_cursor.col; | |
682 while (*p != NUL) | |
1470 | 683 { |
684 #ifdef FEAT_MBYTE | |
685 if (has_mbyte) | |
686 p += replace_push_mb(p); | |
687 else | |
688 #endif | |
689 replace_push(*p++); | |
690 } | |
7 | 691 saved_line[curwin->w_cursor.col] = NUL; |
692 } | |
693 #endif | |
694 | |
695 if ((State & INSERT) | |
696 #ifdef FEAT_VREPLACE | |
697 && !(State & VREPLACE_FLAG) | |
698 #endif | |
699 ) | |
700 { | |
701 p_extra = saved_line + curwin->w_cursor.col; | |
702 #ifdef FEAT_SMARTINDENT | |
703 if (do_si) /* need first char after new line break */ | |
704 { | |
705 p = skipwhite(p_extra); | |
706 first_char = *p; | |
707 } | |
708 #endif | |
709 #ifdef FEAT_COMMENTS | |
710 extra_len = (int)STRLEN(p_extra); | |
711 #endif | |
712 saved_char = *p_extra; | |
713 *p_extra = NUL; | |
714 } | |
715 | |
716 u_clearline(); /* cannot do "U" command when adding lines */ | |
717 #ifdef FEAT_SMARTINDENT | |
718 did_si = FALSE; | |
719 #endif | |
720 ai_col = 0; | |
721 | |
722 /* | |
723 * If we just did an auto-indent, then we didn't type anything on | |
724 * the prior line, and it should be truncated. Do this even if 'ai' is not | |
725 * set because automatically inserting a comment leader also sets did_ai. | |
726 */ | |
727 if (dir == FORWARD && did_ai) | |
728 trunc_line = TRUE; | |
729 | |
730 /* | |
731 * If 'autoindent' and/or 'smartindent' is set, try to figure out what | |
732 * indent to use for the new line. | |
733 */ | |
734 if (curbuf->b_p_ai | |
735 #ifdef FEAT_SMARTINDENT | |
736 || do_si | |
737 #endif | |
738 ) | |
739 { | |
740 /* | |
741 * count white space on current line | |
742 */ | |
5995 | 743 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); |
3584 | 744 if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
745 newindent = second_line_indent; /* for ^^D command in insert mode */ | |
7 | 746 |
747 #ifdef FEAT_SMARTINDENT | |
748 /* | |
749 * Do smart indenting. | |
750 * In insert/replace mode (only when dir == FORWARD) | |
751 * we may move some text to the next line. If it starts with '{' | |
752 * don't add an indent. Fixes inserting a NL before '{' in line | |
753 * "if (condition) {" | |
754 */ | |
755 if (!trunc_line && do_si && *saved_line != NUL | |
756 && (p_extra == NULL || first_char != '{')) | |
757 { | |
758 char_u *ptr; | |
759 char_u last_char; | |
760 | |
761 old_cursor = curwin->w_cursor; | |
762 ptr = saved_line; | |
763 # ifdef FEAT_COMMENTS | |
764 if (flags & OPENLINE_DO_COM) | |
3562 | 765 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
7 | 766 else |
767 lead_len = 0; | |
768 # endif | |
769 if (dir == FORWARD) | |
770 { | |
771 /* | |
772 * Skip preprocessor directives, unless they are | |
773 * recognised as comments. | |
774 */ | |
775 if ( | |
776 # ifdef FEAT_COMMENTS | |
777 lead_len == 0 && | |
778 # endif | |
779 ptr[0] == '#') | |
780 { | |
781 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) | |
782 ptr = ml_get(--curwin->w_cursor.lnum); | |
783 newindent = get_indent(); | |
784 } | |
785 # ifdef FEAT_COMMENTS | |
786 if (flags & OPENLINE_DO_COM) | |
3562 | 787 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
7 | 788 else |
789 lead_len = 0; | |
790 if (lead_len > 0) | |
791 { | |
792 /* | |
793 * This case gets the following right: | |
794 * \* | |
795 * * A comment (read '\' as '/'). | |
796 * *\ | |
797 * #define IN_THE_WAY | |
798 * This should line up here; | |
799 */ | |
800 p = skipwhite(ptr); | |
801 if (p[0] == '/' && p[1] == '*') | |
802 p++; | |
803 if (p[0] == '*') | |
804 { | |
805 for (p++; *p; p++) | |
806 { | |
807 if (p[0] == '/' && p[-1] == '*') | |
808 { | |
809 /* | |
810 * End of C comment, indent should line up | |
811 * with the line containing the start of | |
812 * the comment | |
813 */ | |
814 curwin->w_cursor.col = (colnr_T)(p - ptr); | |
815 if ((pos = findmatch(NULL, NUL)) != NULL) | |
816 { | |
817 curwin->w_cursor.lnum = pos->lnum; | |
818 newindent = get_indent(); | |
819 } | |
820 } | |
821 } | |
822 } | |
823 } | |
824 else /* Not a comment line */ | |
825 # endif | |
826 { | |
827 /* Find last non-blank in line */ | |
828 p = ptr + STRLEN(ptr) - 1; | |
829 while (p > ptr && vim_iswhite(*p)) | |
830 --p; | |
831 last_char = *p; | |
832 | |
833 /* | |
834 * find the character just before the '{' or ';' | |
835 */ | |
836 if (last_char == '{' || last_char == ';') | |
837 { | |
838 if (p > ptr) | |
839 --p; | |
840 while (p > ptr && vim_iswhite(*p)) | |
841 --p; | |
842 } | |
843 /* | |
844 * Try to catch lines that are split over multiple | |
845 * lines. eg: | |
846 * if (condition && | |
847 * condition) { | |
848 * Should line up here! | |
849 * } | |
850 */ | |
851 if (*p == ')') | |
852 { | |
853 curwin->w_cursor.col = (colnr_T)(p - ptr); | |
854 if ((pos = findmatch(NULL, '(')) != NULL) | |
855 { | |
856 curwin->w_cursor.lnum = pos->lnum; | |
857 newindent = get_indent(); | |
858 ptr = ml_get_curline(); | |
859 } | |
860 } | |
861 /* | |
862 * If last character is '{' do indent, without | |
863 * checking for "if" and the like. | |
864 */ | |
865 if (last_char == '{') | |
866 { | |
867 did_si = TRUE; /* do indent */ | |
868 no_si = TRUE; /* don't delete it when '{' typed */ | |
869 } | |
870 /* | |
871 * Look for "if" and the like, use 'cinwords'. | |
872 * Don't do this if the previous line ended in ';' or | |
873 * '}'. | |
874 */ | |
875 else if (last_char != ';' && last_char != '}' | |
876 && cin_is_cinword(ptr)) | |
877 did_si = TRUE; | |
878 } | |
879 } | |
880 else /* dir == BACKWARD */ | |
881 { | |
882 /* | |
883 * Skip preprocessor directives, unless they are | |
884 * recognised as comments. | |
885 */ | |
886 if ( | |
887 # ifdef FEAT_COMMENTS | |
888 lead_len == 0 && | |
889 # endif | |
890 ptr[0] == '#') | |
891 { | |
892 int was_backslashed = FALSE; | |
893 | |
894 while ((ptr[0] == '#' || was_backslashed) && | |
895 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) | |
896 { | |
897 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') | |
898 was_backslashed = TRUE; | |
899 else | |
900 was_backslashed = FALSE; | |
901 ptr = ml_get(++curwin->w_cursor.lnum); | |
902 } | |
903 if (was_backslashed) | |
904 newindent = 0; /* Got to end of file */ | |
905 else | |
906 newindent = get_indent(); | |
907 } | |
908 p = skipwhite(ptr); | |
909 if (*p == '}') /* if line starts with '}': do indent */ | |
910 did_si = TRUE; | |
911 else /* can delete indent when '{' typed */ | |
912 can_si_back = TRUE; | |
913 } | |
914 curwin->w_cursor = old_cursor; | |
915 } | |
916 if (do_si) | |
917 can_si = TRUE; | |
918 #endif /* FEAT_SMARTINDENT */ | |
919 | |
920 did_ai = TRUE; | |
921 } | |
922 | |
923 #ifdef FEAT_COMMENTS | |
924 /* | |
925 * Find out if the current line starts with a comment leader. | |
926 * This may then be inserted in front of the new line. | |
927 */ | |
928 end_comment_pending = NUL; | |
929 if (flags & OPENLINE_DO_COM) | |
3562 | 930 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE); |
7 | 931 else |
932 lead_len = 0; | |
933 if (lead_len > 0) | |
934 { | |
935 char_u *lead_repl = NULL; /* replaces comment leader */ | |
936 int lead_repl_len = 0; /* length of *lead_repl */ | |
937 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ | |
938 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ | |
939 char_u *comment_end = NULL; /* where lead_end has been found */ | |
940 int extra_space = FALSE; /* append extra space */ | |
941 int current_flag; | |
942 int require_blank = FALSE; /* requires blank after middle */ | |
943 char_u *p2; | |
944 | |
945 /* | |
946 * If the comment leader has the start, middle or end flag, it may not | |
947 * be used or may be replaced with the middle leader. | |
948 */ | |
949 for (p = lead_flags; *p && *p != ':'; ++p) | |
950 { | |
951 if (*p == COM_BLANK) | |
952 { | |
953 require_blank = TRUE; | |
954 continue; | |
955 } | |
956 if (*p == COM_START || *p == COM_MIDDLE) | |
957 { | |
958 current_flag = *p; | |
959 if (*p == COM_START) | |
960 { | |
961 /* | |
962 * Doing "O" on a start of comment does not insert leader. | |
963 */ | |
964 if (dir == BACKWARD) | |
965 { | |
966 lead_len = 0; | |
967 break; | |
968 } | |
969 | |
970 /* find start of middle part */ | |
971 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); | |
972 require_blank = FALSE; | |
973 } | |
974 | |
975 /* | |
976 * Isolate the strings of the middle and end leader. | |
977 */ | |
978 while (*p && p[-1] != ':') /* find end of middle flags */ | |
979 { | |
980 if (*p == COM_BLANK) | |
981 require_blank = TRUE; | |
982 ++p; | |
983 } | |
984 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); | |
985 | |
986 while (*p && p[-1] != ':') /* find end of end flags */ | |
987 { | |
988 /* Check whether we allow automatic ending of comments */ | |
989 if (*p == COM_AUTO_END) | |
990 end_comment_pending = -1; /* means we want to set it */ | |
991 ++p; | |
992 } | |
993 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
994 | |
995 if (end_comment_pending == -1) /* we can set it now */ | |
996 end_comment_pending = lead_end[n - 1]; | |
997 | |
998 /* | |
999 * If the end of the comment is in the same line, don't use | |
1000 * the comment leader. | |
1001 */ | |
1002 if (dir == FORWARD) | |
1003 { | |
1004 for (p = saved_line + lead_len; *p; ++p) | |
1005 if (STRNCMP(p, lead_end, n) == 0) | |
1006 { | |
1007 comment_end = p; | |
1008 lead_len = 0; | |
1009 break; | |
1010 } | |
1011 } | |
1012 | |
1013 /* | |
1014 * Doing "o" on a start of comment inserts the middle leader. | |
1015 */ | |
1016 if (lead_len > 0) | |
1017 { | |
1018 if (current_flag == COM_START) | |
1019 { | |
1020 lead_repl = lead_middle; | |
1021 lead_repl_len = (int)STRLEN(lead_middle); | |
1022 } | |
1023 | |
1024 /* | |
1025 * If we have hit RETURN immediately after the start | |
1026 * comment leader, then put a space after the middle | |
1027 * comment leader on the next line. | |
1028 */ | |
1029 if (!vim_iswhite(saved_line[lead_len - 1]) | |
1030 && ((p_extra != NULL | |
1031 && (int)curwin->w_cursor.col == lead_len) | |
1032 || (p_extra == NULL | |
1033 && saved_line[lead_len] == NUL) | |
1034 || require_blank)) | |
1035 extra_space = TRUE; | |
1036 } | |
1037 break; | |
1038 } | |
1039 if (*p == COM_END) | |
1040 { | |
1041 /* | |
1042 * Doing "o" on the end of a comment does not insert leader. | |
1043 * Remember where the end is, might want to use it to find the | |
1044 * start (for C-comments). | |
1045 */ | |
1046 if (dir == FORWARD) | |
1047 { | |
1048 comment_end = skipwhite(saved_line); | |
1049 lead_len = 0; | |
1050 break; | |
1051 } | |
1052 | |
1053 /* | |
1054 * Doing "O" on the end of a comment inserts the middle leader. | |
1055 * Find the string for the middle leader, searching backwards. | |
1056 */ | |
1057 while (p > curbuf->b_p_com && *p != ',') | |
1058 --p; | |
1059 for (lead_repl = p; lead_repl > curbuf->b_p_com | |
1060 && lead_repl[-1] != ':'; --lead_repl) | |
1061 ; | |
1062 lead_repl_len = (int)(p - lead_repl); | |
1063 | |
1064 /* We can probably always add an extra space when doing "O" on | |
1065 * the comment-end */ | |
1066 extra_space = TRUE; | |
1067 | |
1068 /* Check whether we allow automatic ending of comments */ | |
1069 for (p2 = p; *p2 && *p2 != ':'; p2++) | |
1070 { | |
1071 if (*p2 == COM_AUTO_END) | |
1072 end_comment_pending = -1; /* means we want to set it */ | |
1073 } | |
1074 if (end_comment_pending == -1) | |
1075 { | |
1076 /* Find last character in end-comment string */ | |
1077 while (*p2 && *p2 != ',') | |
1078 p2++; | |
1079 end_comment_pending = p2[-1]; | |
1080 } | |
1081 break; | |
1082 } | |
1083 if (*p == COM_FIRST) | |
1084 { | |
1085 /* | |
1086 * Comment leader for first line only: Don't repeat leader | |
1087 * when using "O", blank out leader when using "o". | |
1088 */ | |
1089 if (dir == BACKWARD) | |
1090 lead_len = 0; | |
1091 else | |
1092 { | |
1093 lead_repl = (char_u *)""; | |
1094 lead_repl_len = 0; | |
1095 } | |
1096 break; | |
1097 } | |
1098 } | |
1099 if (lead_len) | |
1100 { | |
3596 | 1101 /* allocate buffer (may concatenate p_extra later) */ |
3584 | 1102 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len |
3596 | 1103 + (second_line_indent > 0 ? second_line_indent : 0) + 1); |
7 | 1104 allocated = leader; /* remember to free it later */ |
1105 | |
1106 if (leader == NULL) | |
1107 lead_len = 0; | |
1108 else | |
1109 { | |
419 | 1110 vim_strncpy(leader, saved_line, lead_len); |
7 | 1111 |
1112 /* | |
1113 * Replace leader with lead_repl, right or left adjusted | |
1114 */ | |
1115 if (lead_repl != NULL) | |
1116 { | |
1117 int c = 0; | |
1118 int off = 0; | |
1119 | |
1996 | 1120 for (p = lead_flags; *p != NUL && *p != ':'; ) |
7 | 1121 { |
1122 if (*p == COM_RIGHT || *p == COM_LEFT) | |
1996 | 1123 c = *p++; |
7 | 1124 else if (VIM_ISDIGIT(*p) || *p == '-') |
1125 off = getdigits(&p); | |
1996 | 1126 else |
1127 ++p; | |
7 | 1128 } |
1129 if (c == COM_RIGHT) /* right adjusted leader */ | |
1130 { | |
1131 /* find last non-white in the leader to line up with */ | |
1132 for (p = leader + lead_len - 1; p > leader | |
1133 && vim_iswhite(*p); --p) | |
1134 ; | |
1135 ++p; | |
17 | 1136 |
1137 #ifdef FEAT_MBYTE | |
1138 /* Compute the length of the replaced characters in | |
1139 * screen characters, not bytes. */ | |
1140 { | |
1141 int repl_size = vim_strnsize(lead_repl, | |
1142 lead_repl_len); | |
1143 int old_size = 0; | |
1144 char_u *endp = p; | |
1145 int l; | |
1146 | |
1147 while (old_size < repl_size && p > leader) | |
1148 { | |
39 | 1149 mb_ptr_back(leader, p); |
17 | 1150 old_size += ptr2cells(p); |
1151 } | |
835 | 1152 l = lead_repl_len - (int)(endp - p); |
17 | 1153 if (l != 0) |
1154 mch_memmove(endp + l, endp, | |
1155 (size_t)((leader + lead_len) - endp)); | |
1156 lead_len += l; | |
1157 } | |
1158 #else | |
7 | 1159 if (p < leader + lead_repl_len) |
1160 p = leader; | |
1161 else | |
1162 p -= lead_repl_len; | |
17 | 1163 #endif |
7 | 1164 mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
1165 if (p + lead_repl_len > leader + lead_len) | |
1166 p[lead_repl_len] = NUL; | |
1167 | |
1168 /* blank-out any other chars from the old leader. */ | |
1169 while (--p >= leader) | |
17 | 1170 { |
1171 #ifdef FEAT_MBYTE | |
1172 int l = mb_head_off(leader, p); | |
1173 | |
1174 if (l > 1) | |
1175 { | |
1176 p -= l; | |
1177 if (ptr2cells(p) > 1) | |
1178 { | |
1179 p[1] = ' '; | |
1180 --l; | |
1181 } | |
1182 mch_memmove(p + 1, p + l + 1, | |
1183 (size_t)((leader + lead_len) - (p + l + 1))); | |
1184 lead_len -= l; | |
1185 *p = ' '; | |
1186 } | |
1187 else | |
1188 #endif | |
7 | 1189 if (!vim_iswhite(*p)) |
1190 *p = ' '; | |
17 | 1191 } |
7 | 1192 } |
1193 else /* left adjusted leader */ | |
1194 { | |
1195 p = skipwhite(leader); | |
17 | 1196 #ifdef FEAT_MBYTE |
1197 /* Compute the length of the replaced characters in | |
1198 * screen characters, not bytes. Move the part that is | |
1199 * not to be overwritten. */ | |
1200 { | |
1201 int repl_size = vim_strnsize(lead_repl, | |
1202 lead_repl_len); | |
1203 int i; | |
1204 int l; | |
1205 | |
1206 for (i = 0; p[i] != NUL && i < lead_len; i += l) | |
1207 { | |
474 | 1208 l = (*mb_ptr2len)(p + i); |
17 | 1209 if (vim_strnsize(p, i + l) > repl_size) |
1210 break; | |
1211 } | |
1212 if (i != lead_repl_len) | |
1213 { | |
1214 mch_memmove(p + lead_repl_len, p + i, | |
2002 | 1215 (size_t)(lead_len - i - (p - leader))); |
17 | 1216 lead_len += lead_repl_len - i; |
1217 } | |
1218 } | |
1219 #endif | |
7 | 1220 mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
1221 | |
1222 /* Replace any remaining non-white chars in the old | |
1223 * leader by spaces. Keep Tabs, the indent must | |
1224 * remain the same. */ | |
1225 for (p += lead_repl_len; p < leader + lead_len; ++p) | |
1226 if (!vim_iswhite(*p)) | |
1227 { | |
1228 /* Don't put a space before a TAB. */ | |
1229 if (p + 1 < leader + lead_len && p[1] == TAB) | |
1230 { | |
1231 --lead_len; | |
1232 mch_memmove(p, p + 1, | |
1233 (leader + lead_len) - p); | |
1234 } | |
1235 else | |
17 | 1236 { |
1237 #ifdef FEAT_MBYTE | |
474 | 1238 int l = (*mb_ptr2len)(p); |
17 | 1239 |
1240 if (l > 1) | |
1241 { | |
1242 if (ptr2cells(p) > 1) | |
1243 { | |
1244 /* Replace a double-wide char with | |
1245 * two spaces */ | |
1246 --l; | |
1247 *p++ = ' '; | |
1248 } | |
1249 mch_memmove(p + 1, p + l, | |
1250 (leader + lead_len) - p); | |
1251 lead_len -= l - 1; | |
1252 } | |
1253 #endif | |
7 | 1254 *p = ' '; |
17 | 1255 } |
7 | 1256 } |
1257 *p = NUL; | |
1258 } | |
1259 | |
1260 /* Recompute the indent, it may have changed. */ | |
1261 if (curbuf->b_p_ai | |
1262 #ifdef FEAT_SMARTINDENT | |
1263 || do_si | |
1264 #endif | |
1265 ) | |
5995 | 1266 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE); |
7 | 1267 |
1268 /* Add the indent offset */ | |
1269 if (newindent + off < 0) | |
1270 { | |
1271 off = -newindent; | |
1272 newindent = 0; | |
1273 } | |
1274 else | |
1275 newindent += off; | |
1276 | |
1277 /* Correct trailing spaces for the shift, so that | |
1278 * alignment remains equal. */ | |
1279 while (off > 0 && lead_len > 0 | |
1280 && leader[lead_len - 1] == ' ') | |
1281 { | |
1282 /* Don't do it when there is a tab before the space */ | |
1283 if (vim_strchr(skipwhite(leader), '\t') != NULL) | |
1284 break; | |
1285 --lead_len; | |
1286 --off; | |
1287 } | |
1288 | |
1289 /* If the leader ends in white space, don't add an | |
1290 * extra space */ | |
1291 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1])) | |
1292 extra_space = FALSE; | |
1293 leader[lead_len] = NUL; | |
1294 } | |
1295 | |
1296 if (extra_space) | |
1297 { | |
1298 leader[lead_len++] = ' '; | |
1299 leader[lead_len] = NUL; | |
1300 } | |
1301 | |
1302 newcol = lead_len; | |
1303 | |
1304 /* | |
1305 * if a new indent will be set below, remove the indent that | |
1306 * is in the comment leader | |
1307 */ | |
1308 if (newindent | |
1309 #ifdef FEAT_SMARTINDENT | |
1310 || did_si | |
1311 #endif | |
1312 ) | |
1313 { | |
1314 while (lead_len && vim_iswhite(*leader)) | |
1315 { | |
1316 --lead_len; | |
1317 --newcol; | |
1318 ++leader; | |
1319 } | |
1320 } | |
1321 | |
1322 } | |
1323 #ifdef FEAT_SMARTINDENT | |
1324 did_si = can_si = FALSE; | |
1325 #endif | |
1326 } | |
1327 else if (comment_end != NULL) | |
1328 { | |
1329 /* | |
1330 * We have finished a comment, so we don't use the leader. | |
1331 * If this was a C-comment and 'ai' or 'si' is set do a normal | |
1332 * indent to align with the line containing the start of the | |
1333 * comment. | |
1334 */ | |
1335 if (comment_end[0] == '*' && comment_end[1] == '/' && | |
1336 (curbuf->b_p_ai | |
1337 #ifdef FEAT_SMARTINDENT | |
1338 || do_si | |
1339 #endif | |
1340 )) | |
1341 { | |
1342 old_cursor = curwin->w_cursor; | |
1343 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); | |
1344 if ((pos = findmatch(NULL, NUL)) != NULL) | |
1345 { | |
1346 curwin->w_cursor.lnum = pos->lnum; | |
1347 newindent = get_indent(); | |
1348 } | |
1349 curwin->w_cursor = old_cursor; | |
1350 } | |
1351 } | |
1352 } | |
1353 #endif | |
1354 | |
1355 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ | |
1356 if (p_extra != NULL) | |
1357 { | |
1358 *p_extra = saved_char; /* restore char that NUL replaced */ | |
1359 | |
1360 /* | |
1361 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first | |
1362 * non-blank. | |
1363 * | |
1364 * When in REPLACE mode, put the deleted blanks on the replace stack, | |
1365 * preceded by a NUL, so they can be put back when a BS is entered. | |
1366 */ | |
1367 if (REPLACE_NORMAL(State)) | |
1368 replace_push(NUL); /* end of extra blanks */ | |
1369 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) | |
1370 { | |
1371 while ((*p_extra == ' ' || *p_extra == '\t') | |
1372 #ifdef FEAT_MBYTE | |
1373 && (!enc_utf8 | |
1374 || !utf_iscomposing(utf_ptr2char(p_extra + 1))) | |
1375 #endif | |
1376 ) | |
1377 { | |
1378 if (REPLACE_NORMAL(State)) | |
1379 replace_push(*p_extra); | |
1380 ++p_extra; | |
1381 ++less_cols_off; | |
1382 } | |
1383 } | |
1384 if (*p_extra != NUL) | |
1385 did_ai = FALSE; /* append some text, don't truncate now */ | |
1386 | |
1387 /* columns for marks adjusted for removed columns */ | |
1388 less_cols = (int)(p_extra - saved_line); | |
1389 } | |
1390 | |
1391 if (p_extra == NULL) | |
1392 p_extra = (char_u *)""; /* append empty line */ | |
1393 | |
1394 #ifdef FEAT_COMMENTS | |
1395 /* concatenate leader and p_extra, if there is a leader */ | |
1396 if (lead_len) | |
1397 { | |
3584 | 1398 if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
1399 { | |
1400 int i; | |
3592 | 1401 int padding = second_line_indent |
1402 - (newindent + (int)STRLEN(leader)); | |
3584 | 1403 |
1404 /* Here whitespace is inserted after the comment char. | |
1405 * Below, set_indent(newindent, SIN_INSERT) will insert the | |
1406 * whitespace needed before the comment char. */ | |
1407 for (i = 0; i < padding; i++) | |
1408 { | |
1409 STRCAT(leader, " "); | |
3705 | 1410 less_cols--; |
3584 | 1411 newcol++; |
1412 } | |
1413 } | |
7 | 1414 STRCAT(leader, p_extra); |
1415 p_extra = leader; | |
1416 did_ai = TRUE; /* So truncating blanks works with comments */ | |
1417 less_cols -= lead_len; | |
1418 } | |
1419 else | |
1420 end_comment_pending = NUL; /* turns out there was no leader */ | |
1421 #endif | |
1422 | |
1423 old_cursor = curwin->w_cursor; | |
1424 if (dir == BACKWARD) | |
1425 --curwin->w_cursor.lnum; | |
1426 #ifdef FEAT_VREPLACE | |
1427 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) | |
1428 #endif | |
1429 { | |
1430 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) | |
1431 == FAIL) | |
1432 goto theend; | |
1433 /* Postpone calling changed_lines(), because it would mess up folding | |
1434 * with markers. */ | |
1435 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); | |
1436 did_append = TRUE; | |
1437 } | |
1438 #ifdef FEAT_VREPLACE | |
1439 else | |
1440 { | |
1441 /* | |
1442 * In VREPLACE mode we are starting to replace the next line. | |
1443 */ | |
1444 curwin->w_cursor.lnum++; | |
1445 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) | |
1446 { | |
1447 /* In case we NL to a new line, BS to the previous one, and NL | |
1448 * again, we don't want to save the new line for undo twice. | |
1449 */ | |
1450 (void)u_save_cursor(); /* errors are ignored! */ | |
1451 vr_lines_changed++; | |
1452 } | |
1453 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); | |
1454 changed_bytes(curwin->w_cursor.lnum, 0); | |
1455 curwin->w_cursor.lnum--; | |
1456 did_append = FALSE; | |
1457 } | |
1458 #endif | |
1459 | |
1460 if (newindent | |
1461 #ifdef FEAT_SMARTINDENT | |
1462 || did_si | |
1463 #endif | |
1464 ) | |
1465 { | |
1466 ++curwin->w_cursor.lnum; | |
1467 #ifdef FEAT_SMARTINDENT | |
1468 if (did_si) | |
1469 { | |
5867 | 1470 int sw = (int)get_sw_value(curbuf); |
3740 | 1471 |
7 | 1472 if (p_sr) |
3740 | 1473 newindent -= newindent % sw; |
1474 newindent += sw; | |
7 | 1475 } |
1476 #endif | |
1324 | 1477 /* Copy the indent */ |
1478 if (curbuf->b_p_ci) | |
7 | 1479 { |
1480 (void)copy_indent(newindent, saved_line); | |
1481 | |
1482 /* | |
1483 * Set the 'preserveindent' option so that any further screwing | |
1484 * with the line doesn't entirely destroy our efforts to preserve | |
1485 * it. It gets restored at the function end. | |
1486 */ | |
1487 curbuf->b_p_pi = TRUE; | |
1488 } | |
1489 else | |
1490 (void)set_indent(newindent, SIN_INSERT); | |
1491 less_cols -= curwin->w_cursor.col; | |
1492 | |
1493 ai_col = curwin->w_cursor.col; | |
1494 | |
1495 /* | |
1496 * In REPLACE mode, for each character in the new indent, there must | |
1497 * be a NUL on the replace stack, for when it is deleted with BS | |
1498 */ | |
1499 if (REPLACE_NORMAL(State)) | |
1500 for (n = 0; n < (int)curwin->w_cursor.col; ++n) | |
1501 replace_push(NUL); | |
1502 newcol += curwin->w_cursor.col; | |
1503 #ifdef FEAT_SMARTINDENT | |
1504 if (no_si) | |
1505 did_si = FALSE; | |
1506 #endif | |
1507 } | |
1508 | |
1509 #ifdef FEAT_COMMENTS | |
1510 /* | |
1511 * In REPLACE mode, for each character in the extra leader, there must be | |
1512 * a NUL on the replace stack, for when it is deleted with BS. | |
1513 */ | |
1514 if (REPLACE_NORMAL(State)) | |
1515 while (lead_len-- > 0) | |
1516 replace_push(NUL); | |
1517 #endif | |
1518 | |
1519 curwin->w_cursor = old_cursor; | |
1520 | |
1521 if (dir == FORWARD) | |
1522 { | |
1523 if (trunc_line || (State & INSERT)) | |
1524 { | |
1525 /* truncate current line at cursor */ | |
1526 saved_line[curwin->w_cursor.col] = NUL; | |
1527 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ | |
1528 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) | |
1529 truncate_spaces(saved_line); | |
1530 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); | |
1531 saved_line = NULL; | |
1532 if (did_append) | |
1533 { | |
1534 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, | |
1535 curwin->w_cursor.lnum + 1, 1L); | |
1536 did_append = FALSE; | |
1537 | |
1538 /* Move marks after the line break to the new line. */ | |
1539 if (flags & OPENLINE_MARKFIX) | |
1540 mark_col_adjust(curwin->w_cursor.lnum, | |
1541 curwin->w_cursor.col + less_cols_off, | |
1542 1L, (long)-less_cols); | |
1543 } | |
1544 else | |
1545 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); | |
1546 } | |
1547 | |
1548 /* | |
1549 * Put the cursor on the new line. Careful: the scrollup() above may | |
1550 * have moved w_cursor, we must use old_cursor. | |
1551 */ | |
1552 curwin->w_cursor.lnum = old_cursor.lnum + 1; | |
1553 } | |
1554 if (did_append) | |
1555 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); | |
1556 | |
1557 curwin->w_cursor.col = newcol; | |
1558 #ifdef FEAT_VIRTUALEDIT | |
1559 curwin->w_cursor.coladd = 0; | |
1560 #endif | |
1561 | |
1562 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
1563 /* | |
1564 * In VREPLACE mode, we are handling the replace stack ourselves, so stop | |
1565 * fixthisline() from doing it (via change_indent()) by telling it we're in | |
1566 * normal INSERT mode. | |
1567 */ | |
1568 if (State & VREPLACE_FLAG) | |
1569 { | |
1570 vreplace_mode = State; /* So we know to put things right later */ | |
1571 State = INSERT; | |
1572 } | |
1573 else | |
1574 vreplace_mode = 0; | |
1575 #endif | |
1576 #ifdef FEAT_LISP | |
1577 /* | |
1578 * May do lisp indenting. | |
1579 */ | |
1580 if (!p_paste | |
1581 # ifdef FEAT_COMMENTS | |
1582 && leader == NULL | |
1583 # endif | |
1584 && curbuf->b_p_lisp | |
1585 && curbuf->b_p_ai) | |
1586 { | |
1587 fixthisline(get_lisp_indent); | |
1588 p = ml_get_curline(); | |
1589 ai_col = (colnr_T)(skipwhite(p) - p); | |
1590 } | |
1591 #endif | |
1592 #ifdef FEAT_CINDENT | |
1593 /* | |
1594 * May do indenting after opening a new line. | |
1595 */ | |
1596 if (!p_paste | |
1597 && (curbuf->b_p_cin | |
1598 # ifdef FEAT_EVAL | |
1599 || *curbuf->b_p_inde != NUL | |
1600 # endif | |
1601 ) | |
1602 && in_cinkeys(dir == FORWARD | |
1603 ? KEY_OPEN_FORW | |
1604 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) | |
1605 { | |
1606 do_c_expr_indent(); | |
1607 p = ml_get_curline(); | |
1608 ai_col = (colnr_T)(skipwhite(p) - p); | |
1609 } | |
1610 #endif | |
1611 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
1612 if (vreplace_mode != 0) | |
1613 State = vreplace_mode; | |
1614 #endif | |
1615 | |
1616 #ifdef FEAT_VREPLACE | |
1617 /* | |
1618 * Finally, VREPLACE gets the stuff on the new line, then puts back the | |
1619 * original line, and inserts the new stuff char by char, pushing old stuff | |
1620 * onto the replace stack (via ins_char()). | |
1621 */ | |
1622 if (State & VREPLACE_FLAG) | |
1623 { | |
1624 /* Put new line in p_extra */ | |
1625 p_extra = vim_strsave(ml_get_curline()); | |
1626 if (p_extra == NULL) | |
1627 goto theend; | |
1628 | |
1629 /* Put back original line */ | |
1630 ml_replace(curwin->w_cursor.lnum, next_line, FALSE); | |
1631 | |
1632 /* Insert new stuff into line again */ | |
1633 curwin->w_cursor.col = 0; | |
1634 #ifdef FEAT_VIRTUALEDIT | |
1635 curwin->w_cursor.coladd = 0; | |
1636 #endif | |
1637 ins_bytes(p_extra); /* will call changed_bytes() */ | |
1638 vim_free(p_extra); | |
1639 next_line = NULL; | |
1640 } | |
1641 #endif | |
1642 | |
1643 retval = TRUE; /* success! */ | |
1644 theend: | |
1645 curbuf->b_p_pi = saved_pi; | |
1646 vim_free(saved_line); | |
1647 vim_free(next_line); | |
1648 vim_free(allocated); | |
1649 return retval; | |
1650 } | |
1651 | |
1652 #if defined(FEAT_COMMENTS) || defined(PROTO) | |
1653 /* | |
5403 | 1654 * get_leader_len() returns the length in bytes of the prefix of the given |
1655 * string which introduces a comment. If this string is not a comment then | |
1656 * 0 is returned. | |
7 | 1657 * When "flags" is not NULL, it is set to point to the flags of the recognized |
1658 * comment leader. | |
1659 * "backward" must be true for the "O" command. | |
3562 | 1660 * If "include_space" is set, include trailing whitespace while calculating the |
1661 * length. | |
7 | 1662 */ |
1663 int | |
3562 | 1664 get_leader_len(line, flags, backward, include_space) |
7 | 1665 char_u *line; |
1666 char_u **flags; | |
1667 int backward; | |
3562 | 1668 int include_space; |
7 | 1669 { |
1670 int i, j; | |
3562 | 1671 int result; |
7 | 1672 int got_com = FALSE; |
1673 int found_one; | |
1674 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ | |
1675 char_u *string; /* pointer to comment string */ | |
1676 char_u *list; | |
2809 | 1677 int middle_match_len = 0; |
1678 char_u *prev_list; | |
2813 | 1679 char_u *saved_flags = NULL; |
7 | 1680 |
3562 | 1681 result = i = 0; |
7 | 1682 while (vim_iswhite(line[i])) /* leading white space is ignored */ |
1683 ++i; | |
1684 | |
1685 /* | |
1686 * Repeat to match several nested comment strings. | |
1687 */ | |
2809 | 1688 while (line[i] != NUL) |
7 | 1689 { |
1690 /* | |
1691 * scan through the 'comments' option for a match | |
1692 */ | |
1693 found_one = FALSE; | |
1694 for (list = curbuf->b_p_com; *list; ) | |
1695 { | |
2809 | 1696 /* Get one option part into part_buf[]. Advance "list" to next |
1697 * one. Put "string" at start of string. */ | |
1698 if (!got_com && flags != NULL) | |
1699 *flags = list; /* remember where flags started */ | |
1700 prev_list = list; | |
7 | 1701 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
1702 string = vim_strchr(part_buf, ':'); | |
1703 if (string == NULL) /* missing ':', ignore this part */ | |
1704 continue; | |
1705 *string++ = NUL; /* isolate flags from string */ | |
1706 | |
2809 | 1707 /* If we found a middle match previously, use that match when this |
1708 * is not a middle or end. */ | |
1709 if (middle_match_len != 0 | |
1710 && vim_strchr(part_buf, COM_MIDDLE) == NULL | |
1711 && vim_strchr(part_buf, COM_END) == NULL) | |
1712 break; | |
1713 | |
1714 /* When we already found a nested comment, only accept further | |
1715 * nested comments. */ | |
7 | 1716 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
1717 continue; | |
1718 | |
2809 | 1719 /* When 'O' flag present and using "O" command skip this one. */ |
7 | 1720 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
1721 continue; | |
1722 | |
2809 | 1723 /* Line contents and string must match. |
7 | 1724 * When string starts with white space, must have some white space |
1725 * (but the amount does not need to match, there might be a mix of | |
2809 | 1726 * TABs and spaces). */ |
7 | 1727 if (vim_iswhite(string[0])) |
1728 { | |
1729 if (i == 0 || !vim_iswhite(line[i - 1])) | |
4352 | 1730 continue; /* missing white space */ |
7 | 1731 while (vim_iswhite(string[0])) |
1732 ++string; | |
1733 } | |
1734 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) | |
1735 ; | |
1736 if (string[j] != NUL) | |
2809 | 1737 continue; /* string doesn't match */ |
1738 | |
1739 /* When 'b' flag used, there must be white space or an | |
1740 * end-of-line after the string in the line. */ | |
7 | 1741 if (vim_strchr(part_buf, COM_BLANK) != NULL |
1742 && !vim_iswhite(line[i + j]) && line[i + j] != NUL) | |
1743 continue; | |
1744 | |
2809 | 1745 /* We have found a match, stop searching unless this is a middle |
1746 * comment. The middle comment can be a substring of the end | |
1747 * comment in which case it's better to return the length of the | |
1748 * end comment and its flags. Thus we keep searching with middle | |
1749 * and end matches and use an end match if it matches better. */ | |
1750 if (vim_strchr(part_buf, COM_MIDDLE) != NULL) | |
1751 { | |
1752 if (middle_match_len == 0) | |
1753 { | |
1754 middle_match_len = j; | |
1755 saved_flags = prev_list; | |
1756 } | |
1757 continue; | |
1758 } | |
1759 if (middle_match_len != 0 && j > middle_match_len) | |
1760 /* Use this match instead of the middle match, since it's a | |
1761 * longer thus better match. */ | |
1762 middle_match_len = 0; | |
1763 | |
1764 if (middle_match_len == 0) | |
1765 i += j; | |
7 | 1766 found_one = TRUE; |
1767 break; | |
1768 } | |
1769 | |
2809 | 1770 if (middle_match_len != 0) |
1771 { | |
1772 /* Use the previously found middle match after failing to find a | |
1773 * match with an end. */ | |
1774 if (!got_com && flags != NULL) | |
1775 *flags = saved_flags; | |
1776 i += middle_match_len; | |
1777 found_one = TRUE; | |
1778 } | |
1779 | |
1780 /* No match found, stop scanning. */ | |
7 | 1781 if (!found_one) |
1782 break; | |
1783 | |
3562 | 1784 result = i; |
1785 | |
2809 | 1786 /* Include any trailing white space. */ |
7 | 1787 while (vim_iswhite(line[i])) |
1788 ++i; | |
1789 | |
3562 | 1790 if (include_space) |
1791 result = i; | |
1792 | |
2809 | 1793 /* If this comment doesn't nest, stop here. */ |
1794 got_com = TRUE; | |
7 | 1795 if (vim_strchr(part_buf, COM_NEST) == NULL) |
1796 break; | |
1797 } | |
3562 | 1798 return result; |
1799 } | |
1800 | |
1801 /* | |
1802 * Return the offset at which the last comment in line starts. If there is no | |
1803 * comment in the whole line, -1 is returned. | |
1804 * | |
1805 * When "flags" is not null, it is set to point to the flags describing the | |
1806 * recognized comment leader. | |
1807 */ | |
1808 int | |
1809 get_last_leader_offset(line, flags) | |
1810 char_u *line; | |
1811 char_u **flags; | |
1812 { | |
1813 int result = -1; | |
1814 int i, j; | |
1815 int lower_check_bound = 0; | |
1816 char_u *string; | |
1817 char_u *com_leader; | |
1818 char_u *com_flags; | |
1819 char_u *list; | |
1820 int found_one; | |
1821 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ | |
1822 | |
1823 /* | |
1824 * Repeat to match several nested comment strings. | |
1825 */ | |
1826 i = (int)STRLEN(line); | |
1827 while (--i >= lower_check_bound) | |
1828 { | |
1829 /* | |
1830 * scan through the 'comments' option for a match | |
1831 */ | |
1832 found_one = FALSE; | |
1833 for (list = curbuf->b_p_com; *list; ) | |
1834 { | |
1835 char_u *flags_save = list; | |
1836 | |
1837 /* | |
1838 * Get one option part into part_buf[]. Advance list to next one. | |
1839 * put string at start of string. | |
1840 */ | |
1841 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); | |
1842 string = vim_strchr(part_buf, ':'); | |
1843 if (string == NULL) /* If everything is fine, this cannot actually | |
1844 * happen. */ | |
1845 { | |
1846 continue; | |
1847 } | |
1848 *string++ = NUL; /* Isolate flags from string. */ | |
1849 com_leader = string; | |
1850 | |
1851 /* | |
1852 * Line contents and string must match. | |
1853 * When string starts with white space, must have some white space | |
1854 * (but the amount does not need to match, there might be a mix of | |
1855 * TABs and spaces). | |
1856 */ | |
1857 if (vim_iswhite(string[0])) | |
1858 { | |
1859 if (i == 0 || !vim_iswhite(line[i - 1])) | |
1860 continue; | |
1861 while (vim_iswhite(string[0])) | |
1862 ++string; | |
1863 } | |
1864 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) | |
1865 /* do nothing */; | |
1866 if (string[j] != NUL) | |
1867 continue; | |
1868 | |
1869 /* | |
1870 * When 'b' flag used, there must be white space or an | |
1871 * end-of-line after the string in the line. | |
1872 */ | |
1873 if (vim_strchr(part_buf, COM_BLANK) != NULL | |
1874 && !vim_iswhite(line[i + j]) && line[i + j] != NUL) | |
1875 { | |
1876 continue; | |
1877 } | |
1878 | |
1879 /* | |
1880 * We have found a match, stop searching. | |
1881 */ | |
1882 found_one = TRUE; | |
1883 | |
1884 if (flags) | |
1885 *flags = flags_save; | |
1886 com_flags = flags_save; | |
1887 | |
1888 break; | |
1889 } | |
1890 | |
1891 if (found_one) | |
1892 { | |
1893 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ | |
1894 int len1, len2, off; | |
1895 | |
1896 result = i; | |
1897 /* | |
1898 * If this comment nests, continue searching. | |
1899 */ | |
1900 if (vim_strchr(part_buf, COM_NEST) != NULL) | |
1901 continue; | |
1902 | |
1903 lower_check_bound = i; | |
1904 | |
1905 /* Let's verify whether the comment leader found is a substring | |
1906 * of other comment leaders. If it is, let's adjust the | |
1907 * lower_check_bound so that we make sure that we have determined | |
1908 * the comment leader correctly. | |
1909 */ | |
1910 | |
1911 while (vim_iswhite(*com_leader)) | |
1912 ++com_leader; | |
1913 len1 = (int)STRLEN(com_leader); | |
1914 | |
1915 for (list = curbuf->b_p_com; *list; ) | |
1916 { | |
1917 char_u *flags_save = list; | |
1918 | |
1919 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); | |
1920 if (flags_save == com_flags) | |
1921 continue; | |
1922 string = vim_strchr(part_buf2, ':'); | |
1923 ++string; | |
1924 while (vim_iswhite(*string)) | |
1925 ++string; | |
1926 len2 = (int)STRLEN(string); | |
1927 if (len2 == 0) | |
1928 continue; | |
1929 | |
1930 /* Now we have to verify whether string ends with a substring | |
1931 * beginning the com_leader. */ | |
1932 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) | |
1933 { | |
1934 --off; | |
1935 if (!STRNCMP(string + off, com_leader, len2 - off)) | |
1936 { | |
1937 if (i - off < lower_check_bound) | |
1938 lower_check_bound = i - off; | |
1939 } | |
1940 } | |
1941 } | |
1942 } | |
1943 } | |
1944 return result; | |
7 | 1945 } |
1946 #endif | |
1947 | |
1948 /* | |
1949 * Return the number of window lines occupied by buffer line "lnum". | |
1950 */ | |
1951 int | |
1952 plines(lnum) | |
1953 linenr_T lnum; | |
1954 { | |
1955 return plines_win(curwin, lnum, TRUE); | |
1956 } | |
1957 | |
1958 int | |
1959 plines_win(wp, lnum, winheight) | |
1960 win_T *wp; | |
1961 linenr_T lnum; | |
1962 int winheight; /* when TRUE limit to window height */ | |
1963 { | |
1964 #if defined(FEAT_DIFF) || defined(PROTO) | |
1965 /* Check for filler lines above this buffer line. When folded the result | |
1966 * is one line anyway. */ | |
1967 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); | |
1968 } | |
1969 | |
1970 int | |
1971 plines_nofill(lnum) | |
1972 linenr_T lnum; | |
1973 { | |
1974 return plines_win_nofill(curwin, lnum, TRUE); | |
1975 } | |
1976 | |
1977 int | |
1978 plines_win_nofill(wp, lnum, winheight) | |
1979 win_T *wp; | |
1980 linenr_T lnum; | |
1981 int winheight; /* when TRUE limit to window height */ | |
1982 { | |
1983 #endif | |
1984 int lines; | |
1985 | |
1986 if (!wp->w_p_wrap) | |
1987 return 1; | |
1988 | |
1989 #ifdef FEAT_VERTSPLIT | |
1990 if (wp->w_width == 0) | |
1991 return 1; | |
1992 #endif | |
1993 | |
1994 #ifdef FEAT_FOLDING | |
1995 /* A folded lines is handled just like an empty line. */ | |
1996 /* NOTE: Caller must handle lines that are MAYBE folded. */ | |
1997 if (lineFolded(wp, lnum) == TRUE) | |
1998 return 1; | |
1999 #endif | |
2000 | |
2001 lines = plines_win_nofold(wp, lnum); | |
2002 if (winheight > 0 && lines > wp->w_height) | |
2003 return (int)wp->w_height; | |
2004 return lines; | |
2005 } | |
2006 | |
2007 /* | |
2008 * Return number of window lines physical line "lnum" will occupy in window | |
2009 * "wp". Does not care about folding, 'wrap' or 'diff'. | |
2010 */ | |
2011 int | |
2012 plines_win_nofold(wp, lnum) | |
2013 win_T *wp; | |
2014 linenr_T lnum; | |
2015 { | |
2016 char_u *s; | |
2017 long col; | |
2018 int width; | |
2019 | |
2020 s = ml_get_buf(wp->w_buffer, lnum, FALSE); | |
2021 if (*s == NUL) /* empty line */ | |
2022 return 1; | |
2023 col = win_linetabsize(wp, s, (colnr_T)MAXCOL); | |
2024 | |
2025 /* | |
2026 * If list mode is on, then the '$' at the end of the line may take up one | |
2027 * extra column. | |
2028 */ | |
2029 if (wp->w_p_list && lcs_eol != NUL) | |
2030 col += 1; | |
2031 | |
2032 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
2033 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
7 | 2034 */ |
2035 width = W_WIDTH(wp) - win_col_off(wp); | |
2036 if (width <= 0) | |
2037 return 32000; | |
2038 if (col <= width) | |
2039 return 1; | |
2040 col -= width; | |
2041 width += win_col_off2(wp); | |
2042 return (col + (width - 1)) / width + 1; | |
2043 } | |
2044 | |
2045 /* | |
2046 * Like plines_win(), but only reports the number of physical screen lines | |
2047 * used from the start of the line to the given column number. | |
2048 */ | |
2049 int | |
2050 plines_win_col(wp, lnum, column) | |
2051 win_T *wp; | |
2052 linenr_T lnum; | |
2053 long column; | |
2054 { | |
2055 long col; | |
2056 char_u *s; | |
2057 int lines = 0; | |
2058 int width; | |
5995 | 2059 char_u *line; |
7 | 2060 |
2061 #ifdef FEAT_DIFF | |
2062 /* Check for filler lines above this buffer line. When folded the result | |
2063 * is one line anyway. */ | |
2064 lines = diff_check_fill(wp, lnum); | |
2065 #endif | |
2066 | |
2067 if (!wp->w_p_wrap) | |
2068 return lines + 1; | |
2069 | |
2070 #ifdef FEAT_VERTSPLIT | |
2071 if (wp->w_width == 0) | |
2072 return lines + 1; | |
2073 #endif | |
2074 | |
5995 | 2075 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
7 | 2076 |
2077 col = 0; | |
2078 while (*s != NUL && --column >= 0) | |
2079 { | |
5995 | 2080 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL); |
39 | 2081 mb_ptr_adv(s); |
7 | 2082 } |
2083 | |
2084 /* | |
2085 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in | |
2086 * INSERT mode, then col must be adjusted so that it represents the last | |
2087 * screen position of the TAB. This only fixes an error when the TAB wraps | |
2088 * from one screen line to the next (when 'columns' is not a multiple of | |
2089 * 'ts') -- webb. | |
2090 */ | |
2091 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) | |
5995 | 2092 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1; |
7 | 2093 |
2094 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
2095 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
7 | 2096 */ |
2097 width = W_WIDTH(wp) - win_col_off(wp); | |
1023 | 2098 if (width <= 0) |
2099 return 9999; | |
2100 | |
2101 lines += 1; | |
2102 if (col > width) | |
2103 lines += (col - width) / (width + win_col_off2(wp)) + 1; | |
2104 return lines; | |
7 | 2105 } |
2106 | |
2107 int | |
2108 plines_m_win(wp, first, last) | |
2109 win_T *wp; | |
2110 linenr_T first, last; | |
2111 { | |
2112 int count = 0; | |
2113 | |
2114 while (first <= last) | |
2115 { | |
2116 #ifdef FEAT_FOLDING | |
2117 int x; | |
2118 | |
2119 /* Check if there are any really folded lines, but also included lines | |
2120 * that are maybe folded. */ | |
2121 x = foldedCount(wp, first, NULL); | |
2122 if (x > 0) | |
2123 { | |
2124 ++count; /* count 1 for "+-- folded" line */ | |
2125 first += x; | |
2126 } | |
2127 else | |
2128 #endif | |
2129 { | |
2130 #ifdef FEAT_DIFF | |
2131 if (first == wp->w_topline) | |
2132 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; | |
2133 else | |
2134 #endif | |
2135 count += plines_win(wp, first, TRUE); | |
2136 ++first; | |
2137 } | |
2138 } | |
2139 return (count); | |
2140 } | |
2141 | |
2142 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO) | |
2143 /* | |
2144 * Insert string "p" at the cursor position. Stops at a NUL byte. | |
2145 * Handles Replace mode and multi-byte characters. | |
2146 */ | |
2147 void | |
2148 ins_bytes(p) | |
2149 char_u *p; | |
2150 { | |
2151 ins_bytes_len(p, (int)STRLEN(p)); | |
2152 } | |
2153 #endif | |
2154 | |
2155 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ | |
2156 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO) | |
2157 /* | |
2158 * Insert string "p" with length "len" at the cursor position. | |
2159 * Handles Replace mode and multi-byte characters. | |
2160 */ | |
2161 void | |
2162 ins_bytes_len(p, len) | |
2163 char_u *p; | |
2164 int len; | |
2165 { | |
2166 int i; | |
2167 # ifdef FEAT_MBYTE | |
2168 int n; | |
2169 | |
1617 | 2170 if (has_mbyte) |
2171 for (i = 0; i < len; i += n) | |
2172 { | |
2173 if (enc_utf8) | |
2174 /* avoid reading past p[len] */ | |
2175 n = utfc_ptr2len_len(p + i, len - i); | |
2176 else | |
2177 n = (*mb_ptr2len)(p + i); | |
2178 ins_char_bytes(p + i, n); | |
2179 } | |
2180 else | |
7 | 2181 # endif |
1617 | 2182 for (i = 0; i < len; ++i) |
2183 ins_char(p[i]); | |
7 | 2184 } |
2185 #endif | |
2186 | |
2187 /* | |
2188 * Insert or replace a single character at the cursor position. | |
2189 * When in REPLACE or VREPLACE mode, replace any existing character. | |
2190 * Caller must have prepared for undo. | |
2191 * For multi-byte characters we get the whole character, the caller must | |
2192 * convert bytes to a character. | |
2193 */ | |
2194 void | |
2195 ins_char(c) | |
2196 int c; | |
2197 { | |
2198 #if defined(FEAT_MBYTE) || defined(PROTO) | |
3549 | 2199 char_u buf[MB_MAXBYTES + 1]; |
7 | 2200 int n; |
2201 | |
2202 n = (*mb_char2bytes)(c, buf); | |
2203 | |
2204 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. | |
2205 * Happens for CTRL-Vu9900. */ | |
2206 if (buf[0] == 0) | |
2207 buf[0] = '\n'; | |
2208 | |
2209 ins_char_bytes(buf, n); | |
2210 } | |
2211 | |
2212 void | |
2213 ins_char_bytes(buf, charlen) | |
2214 char_u *buf; | |
2215 int charlen; | |
2216 { | |
2217 int c = buf[0]; | |
2218 #endif | |
2219 int newlen; /* nr of bytes inserted */ | |
2220 int oldlen; /* nr of bytes deleted (0 when not replacing) */ | |
2221 char_u *p; | |
2222 char_u *newp; | |
2223 char_u *oldp; | |
2224 int linelen; /* length of old line including NUL */ | |
2225 colnr_T col; | |
2226 linenr_T lnum = curwin->w_cursor.lnum; | |
2227 int i; | |
2228 | |
2229 #ifdef FEAT_VIRTUALEDIT | |
2230 /* Break tabs if needed. */ | |
2231 if (virtual_active() && curwin->w_cursor.coladd > 0) | |
2232 coladvance_force(getviscol()); | |
2233 #endif | |
2234 | |
2235 col = curwin->w_cursor.col; | |
2236 oldp = ml_get(lnum); | |
2237 linelen = (int)STRLEN(oldp) + 1; | |
2238 | |
2239 /* The lengths default to the values for when not replacing. */ | |
2240 oldlen = 0; | |
2241 #ifdef FEAT_MBYTE | |
2242 newlen = charlen; | |
2243 #else | |
2244 newlen = 1; | |
2245 #endif | |
2246 | |
2247 if (State & REPLACE_FLAG) | |
2248 { | |
2249 #ifdef FEAT_VREPLACE | |
2250 if (State & VREPLACE_FLAG) | |
2251 { | |
2252 colnr_T new_vcol = 0; /* init for GCC */ | |
2253 colnr_T vcol; | |
2254 int old_list; | |
2255 #ifndef FEAT_MBYTE | |
2256 char_u buf[2]; | |
2257 #endif | |
2258 | |
2259 /* | |
2260 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. | |
2261 * Returns the old value of list, so when finished, | |
2262 * curwin->w_p_list should be set back to this. | |
2263 */ | |
2264 old_list = curwin->w_p_list; | |
2265 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) | |
2266 curwin->w_p_list = FALSE; | |
2267 | |
2268 /* | |
2269 * In virtual replace mode each character may replace one or more | |
2270 * characters (zero if it's a TAB). Count the number of bytes to | |
2271 * be deleted to make room for the new character, counting screen | |
2272 * cells. May result in adding spaces to fill a gap. | |
2273 */ | |
2274 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); | |
2275 #ifndef FEAT_MBYTE | |
2276 buf[0] = c; | |
2277 buf[1] = NUL; | |
2278 #endif | |
2279 new_vcol = vcol + chartabsize(buf, vcol); | |
2280 while (oldp[col + oldlen] != NUL && vcol < new_vcol) | |
2281 { | |
2282 vcol += chartabsize(oldp + col + oldlen, vcol); | |
2283 /* Don't need to remove a TAB that takes us to the right | |
2284 * position. */ | |
2285 if (vcol > new_vcol && oldp[col + oldlen] == TAB) | |
2286 break; | |
2287 #ifdef FEAT_MBYTE | |
474 | 2288 oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
7 | 2289 #else |
2290 ++oldlen; | |
2291 #endif | |
2292 /* Deleted a bit too much, insert spaces. */ | |
2293 if (vcol > new_vcol) | |
2294 newlen += vcol - new_vcol; | |
2295 } | |
2296 curwin->w_p_list = old_list; | |
2297 } | |
2298 else | |
2299 #endif | |
2300 if (oldp[col] != NUL) | |
2301 { | |
2302 /* normal replace */ | |
2303 #ifdef FEAT_MBYTE | |
474 | 2304 oldlen = (*mb_ptr2len)(oldp + col); |
7 | 2305 #else |
2306 oldlen = 1; | |
2307 #endif | |
2308 } | |
2309 | |
2310 | |
2311 /* Push the replaced bytes onto the replace stack, so that they can be | |
2312 * put back when BS is used. The bytes of a multi-byte character are | |
2313 * done the other way around, so that the first byte is popped off | |
2314 * first (it tells the byte length of the character). */ | |
2315 replace_push(NUL); | |
2316 for (i = 0; i < oldlen; ++i) | |
2317 { | |
2318 #ifdef FEAT_MBYTE | |
1470 | 2319 if (has_mbyte) |
2320 i += replace_push_mb(oldp + col + i) - 1; | |
2321 else | |
2322 #endif | |
2323 replace_push(oldp[col + i]); | |
7 | 2324 } |
2325 } | |
2326 | |
2327 newp = alloc_check((unsigned)(linelen + newlen - oldlen)); | |
2328 if (newp == NULL) | |
2329 return; | |
2330 | |
2331 /* Copy bytes before the cursor. */ | |
2332 if (col > 0) | |
2333 mch_memmove(newp, oldp, (size_t)col); | |
2334 | |
2335 /* Copy bytes after the changed character(s). */ | |
2336 p = newp + col; | |
2337 mch_memmove(p + newlen, oldp + col + oldlen, | |
2338 (size_t)(linelen - col - oldlen)); | |
2339 | |
2340 /* Insert or overwrite the new character. */ | |
2341 #ifdef FEAT_MBYTE | |
2342 mch_memmove(p, buf, charlen); | |
2343 i = charlen; | |
2344 #else | |
2345 *p = c; | |
2346 i = 1; | |
2347 #endif | |
2348 | |
2349 /* Fill with spaces when necessary. */ | |
2350 while (i < newlen) | |
2351 p[i++] = ' '; | |
2352 | |
2353 /* Replace the line in the buffer. */ | |
2354 ml_replace(lnum, newp, FALSE); | |
2355 | |
2356 /* mark the buffer as changed and prepare for displaying */ | |
2357 changed_bytes(lnum, col); | |
2358 | |
2359 /* | |
2360 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly | |
2361 * show the match for right parens and braces. | |
2362 */ | |
2363 if (p_sm && (State & INSERT) | |
2364 && msg_silent == 0 | |
674 | 2365 #ifdef FEAT_INS_EXPAND |
2366 && !ins_compl_active() | |
2367 #endif | |
7 | 2368 ) |
4029 | 2369 { |
2370 #ifdef FEAT_MBYTE | |
2371 if (has_mbyte) | |
2372 showmatch(mb_ptr2char(buf)); | |
2373 else | |
2374 #endif | |
2375 showmatch(c); | |
2376 } | |
7 | 2377 |
2378 #ifdef FEAT_RIGHTLEFT | |
2379 if (!p_ri || (State & REPLACE_FLAG)) | |
2380 #endif | |
2381 { | |
2382 /* Normal insert: move cursor right */ | |
2383 #ifdef FEAT_MBYTE | |
2384 curwin->w_cursor.col += charlen; | |
2385 #else | |
2386 ++curwin->w_cursor.col; | |
2387 #endif | |
2388 } | |
2389 /* | |
2390 * TODO: should try to update w_row here, to avoid recomputing it later. | |
2391 */ | |
2392 } | |
2393 | |
2394 /* | |
2395 * Insert a string at the cursor position. | |
2396 * Note: Does NOT handle Replace mode. | |
2397 * Caller must have prepared for undo. | |
2398 */ | |
2399 void | |
2400 ins_str(s) | |
2401 char_u *s; | |
2402 { | |
2403 char_u *oldp, *newp; | |
2404 int newlen = (int)STRLEN(s); | |
2405 int oldlen; | |
2406 colnr_T col; | |
2407 linenr_T lnum = curwin->w_cursor.lnum; | |
2408 | |
2409 #ifdef FEAT_VIRTUALEDIT | |
2410 if (virtual_active() && curwin->w_cursor.coladd > 0) | |
2411 coladvance_force(getviscol()); | |
2412 #endif | |
2413 | |
2414 col = curwin->w_cursor.col; | |
2415 oldp = ml_get(lnum); | |
2416 oldlen = (int)STRLEN(oldp); | |
2417 | |
2418 newp = alloc_check((unsigned)(oldlen + newlen + 1)); | |
2419 if (newp == NULL) | |
2420 return; | |
2421 if (col > 0) | |
2422 mch_memmove(newp, oldp, (size_t)col); | |
2423 mch_memmove(newp + col, s, (size_t)newlen); | |
2424 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); | |
2425 ml_replace(lnum, newp, FALSE); | |
2426 changed_bytes(lnum, col); | |
2427 curwin->w_cursor.col += newlen; | |
2428 } | |
2429 | |
2430 /* | |
2431 * Delete one character under the cursor. | |
2432 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. | |
2433 * Caller must have prepared for undo. | |
2434 * | |
2435 * return FAIL for failure, OK otherwise | |
2436 */ | |
2437 int | |
2438 del_char(fixpos) | |
2439 int fixpos; | |
2440 { | |
2441 #ifdef FEAT_MBYTE | |
2442 if (has_mbyte) | |
2443 { | |
2444 /* Make sure the cursor is at the start of a character. */ | |
2445 mb_adjust_cursor(); | |
2446 if (*ml_get_cursor() == NUL) | |
2447 return FAIL; | |
2448 return del_chars(1L, fixpos); | |
2449 } | |
2450 #endif | |
610 | 2451 return del_bytes(1L, fixpos, TRUE); |
7 | 2452 } |
2453 | |
2454 #if defined(FEAT_MBYTE) || defined(PROTO) | |
2455 /* | |
2456 * Like del_bytes(), but delete characters instead of bytes. | |
2457 */ | |
2458 int | |
2459 del_chars(count, fixpos) | |
2460 long count; | |
2461 int fixpos; | |
2462 { | |
2463 long bytes = 0; | |
2464 long i; | |
2465 char_u *p; | |
2466 int l; | |
2467 | |
2468 p = ml_get_cursor(); | |
2469 for (i = 0; i < count && *p != NUL; ++i) | |
2470 { | |
474 | 2471 l = (*mb_ptr2len)(p); |
7 | 2472 bytes += l; |
2473 p += l; | |
2474 } | |
610 | 2475 return del_bytes(bytes, fixpos, TRUE); |
7 | 2476 } |
2477 #endif | |
2478 | |
2479 /* | |
2480 * Delete "count" bytes under the cursor. | |
2481 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. | |
2482 * Caller must have prepared for undo. | |
2483 * | |
2484 * return FAIL for failure, OK otherwise | |
2485 */ | |
2486 int | |
777 | 2487 del_bytes(count, fixpos_arg, use_delcombine) |
7 | 2488 long count; |
777 | 2489 int fixpos_arg; |
1877 | 2490 int use_delcombine UNUSED; /* 'delcombine' option applies */ |
7 | 2491 { |
2492 char_u *oldp, *newp; | |
2493 colnr_T oldlen; | |
2494 linenr_T lnum = curwin->w_cursor.lnum; | |
2495 colnr_T col = curwin->w_cursor.col; | |
2496 int was_alloced; | |
2497 long movelen; | |
777 | 2498 int fixpos = fixpos_arg; |
7 | 2499 |
2500 oldp = ml_get(lnum); | |
2501 oldlen = (int)STRLEN(oldp); | |
2502 | |
2503 /* | |
2504 * Can't do anything when the cursor is on the NUL after the line. | |
2505 */ | |
2506 if (col >= oldlen) | |
2507 return FAIL; | |
2508 | |
2509 #ifdef FEAT_MBYTE | |
2510 /* If 'delcombine' is set and deleting (less than) one character, only | |
2511 * delete the last combining character. */ | |
610 | 2512 if (p_deco && use_delcombine && enc_utf8 |
2513 && utfc_ptr2len(oldp + col) >= count) | |
7 | 2514 { |
714 | 2515 int cc[MAX_MCO]; |
7 | 2516 int n; |
2517 | |
714 | 2518 (void)utfc_ptr2char(oldp + col, cc); |
2519 if (cc[0] != NUL) | |
7 | 2520 { |
2521 /* Find the last composing char, there can be several. */ | |
2522 n = col; | |
2523 do | |
2524 { | |
2525 col = n; | |
474 | 2526 count = utf_ptr2len(oldp + n); |
7 | 2527 n += count; |
2528 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); | |
2529 fixpos = 0; | |
2530 } | |
2531 } | |
2532 #endif | |
2533 | |
2534 /* | |
2535 * When count is too big, reduce it. | |
2536 */ | |
2537 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ | |
2538 if (movelen <= 1) | |
2539 { | |
2540 /* | |
2541 * If we just took off the last character of a non-blank line, and | |
777 | 2542 * fixpos is TRUE, we don't want to end up positioned at the NUL, |
2543 * unless "restart_edit" is set or 'virtualedit' contains "onemore". | |
7 | 2544 */ |
777 | 2545 if (col > 0 && fixpos && restart_edit == 0 |
2546 #ifdef FEAT_VIRTUALEDIT | |
2547 && (ve_flags & VE_ONEMORE) == 0 | |
2548 #endif | |
2549 ) | |
7 | 2550 { |
2551 --curwin->w_cursor.col; | |
2552 #ifdef FEAT_VIRTUALEDIT | |
2553 curwin->w_cursor.coladd = 0; | |
2554 #endif | |
2555 #ifdef FEAT_MBYTE | |
2556 if (has_mbyte) | |
2557 curwin->w_cursor.col -= | |
2558 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); | |
2559 #endif | |
2560 } | |
2561 count = oldlen - col; | |
2562 movelen = 1; | |
2563 } | |
2564 | |
2565 /* | |
2566 * If the old line has been allocated the deletion can be done in the | |
2567 * existing line. Otherwise a new line has to be allocated | |
1532 | 2568 * Can't do this when using Netbeans, because we would need to invoke |
2569 * 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
|
2570 * care of notifying Netbeans. |
7 | 2571 */ |
2572 #ifdef FEAT_NETBEANS_INTG | |
2210 | 2573 if (netbeans_active()) |
1532 | 2574 was_alloced = FALSE; |
2575 else | |
2576 #endif | |
2577 was_alloced = ml_line_alloced(); /* check if oldp was allocated */ | |
7 | 2578 if (was_alloced) |
2579 newp = oldp; /* use same allocated memory */ | |
2580 else | |
2581 { /* need to allocate a new line */ | |
2582 newp = alloc((unsigned)(oldlen + 1 - count)); | |
2583 if (newp == NULL) | |
2584 return FAIL; | |
2585 mch_memmove(newp, oldp, (size_t)col); | |
2586 } | |
2587 mch_memmove(newp + col, oldp + col + count, (size_t)movelen); | |
2588 if (!was_alloced) | |
2589 ml_replace(lnum, newp, FALSE); | |
2590 | |
2591 /* mark the buffer as changed and prepare for displaying */ | |
2592 changed_bytes(lnum, curwin->w_cursor.col); | |
2593 | |
2594 return OK; | |
2595 } | |
2596 | |
2597 /* | |
2598 * Delete from cursor to end of line. | |
2599 * Caller must have prepared for undo. | |
2600 * | |
2601 * return FAIL for failure, OK otherwise | |
2602 */ | |
2603 int | |
2604 truncate_line(fixpos) | |
2605 int fixpos; /* if TRUE fix the cursor position when done */ | |
2606 { | |
2607 char_u *newp; | |
2608 linenr_T lnum = curwin->w_cursor.lnum; | |
2609 colnr_T col = curwin->w_cursor.col; | |
2610 | |
2611 if (col == 0) | |
2612 newp = vim_strsave((char_u *)""); | |
2613 else | |
2614 newp = vim_strnsave(ml_get(lnum), col); | |
2615 | |
2616 if (newp == NULL) | |
2617 return FAIL; | |
2618 | |
2619 ml_replace(lnum, newp, FALSE); | |
2620 | |
2621 /* mark the buffer as changed and prepare for displaying */ | |
2622 changed_bytes(lnum, curwin->w_cursor.col); | |
2623 | |
2624 /* | |
2625 * If "fixpos" is TRUE we don't want to end up positioned at the NUL. | |
2626 */ | |
2627 if (fixpos && curwin->w_cursor.col > 0) | |
2628 --curwin->w_cursor.col; | |
2629 | |
2630 return OK; | |
2631 } | |
2632 | |
2633 /* | |
2634 * Delete "nlines" lines at the cursor. | |
2635 * Saves the lines for undo first if "undo" is TRUE. | |
2636 */ | |
2637 void | |
2638 del_lines(nlines, undo) | |
2639 long nlines; /* number of lines to delete */ | |
2640 int undo; /* if TRUE, prepare for undo */ | |
2641 { | |
2642 long n; | |
1929 | 2643 linenr_T first = curwin->w_cursor.lnum; |
7 | 2644 |
2645 if (nlines <= 0) | |
2646 return; | |
2647 | |
2648 /* save the deleted lines for undo */ | |
1929 | 2649 if (undo && u_savedel(first, nlines) == FAIL) |
7 | 2650 return; |
2651 | |
2652 for (n = 0; n < nlines; ) | |
2653 { | |
2654 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ | |
2655 break; | |
2656 | |
1929 | 2657 ml_delete(first, TRUE); |
7 | 2658 ++n; |
2659 | |
2660 /* If we delete the last line in the file, stop */ | |
1929 | 2661 if (first > curbuf->b_ml.ml_line_count) |
7 | 2662 break; |
2663 } | |
1929 | 2664 |
2665 /* Correct the cursor position before calling deleted_lines_mark(), it may | |
2666 * trigger a callback to display the cursor. */ | |
7 | 2667 curwin->w_cursor.col = 0; |
2668 check_cursor_lnum(); | |
1929 | 2669 |
2670 /* adjust marks, mark the buffer as changed and prepare for displaying */ | |
2671 deleted_lines_mark(first, n); | |
7 | 2672 } |
2673 | |
2674 int | |
2675 gchar_pos(pos) | |
2676 pos_T *pos; | |
2677 { | |
2678 char_u *ptr = ml_get_pos(pos); | |
2679 | |
2680 #ifdef FEAT_MBYTE | |
2681 if (has_mbyte) | |
2682 return (*mb_ptr2char)(ptr); | |
2683 #endif | |
2684 return (int)*ptr; | |
2685 } | |
2686 | |
2687 int | |
2688 gchar_cursor() | |
2689 { | |
2690 #ifdef FEAT_MBYTE | |
2691 if (has_mbyte) | |
2692 return (*mb_ptr2char)(ml_get_cursor()); | |
2693 #endif | |
2694 return (int)*ml_get_cursor(); | |
2695 } | |
2696 | |
2697 /* | |
2698 * Write a character at the current cursor position. | |
2699 * It is directly written into the block. | |
2700 */ | |
2701 void | |
2702 pchar_cursor(c) | |
2703 int c; | |
2704 { | |
2705 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) | |
2706 + curwin->w_cursor.col) = c; | |
2707 } | |
2708 | |
2709 /* | |
2710 * When extra == 0: Return TRUE if the cursor is before or on the first | |
2711 * non-blank in the line. | |
2712 * When extra == 1: Return TRUE if the cursor is before the first non-blank in | |
2713 * the line. | |
2714 */ | |
2715 int | |
2716 inindent(extra) | |
2717 int extra; | |
2718 { | |
2719 char_u *ptr; | |
2720 colnr_T col; | |
2721 | |
2722 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col) | |
2723 ++ptr; | |
2724 if (col >= curwin->w_cursor.col + extra) | |
2725 return TRUE; | |
2726 else | |
2727 return FALSE; | |
2728 } | |
2729 | |
2730 /* | |
2731 * Skip to next part of an option argument: Skip space and comma. | |
2732 */ | |
2733 char_u * | |
2734 skip_to_option_part(p) | |
2735 char_u *p; | |
2736 { | |
2737 if (*p == ',') | |
2738 ++p; | |
2739 while (*p == ' ') | |
2740 ++p; | |
2741 return p; | |
2742 } | |
2743 | |
2744 /* | |
2189 | 2745 * Call this function when something in the current buffer is changed. |
7 | 2746 * |
2747 * Most often called through changed_bytes() and changed_lines(), which also | |
2748 * mark the area of the display to be redrawn. | |
2189 | 2749 * |
2750 * Careful: may trigger autocommands that reload the buffer. | |
7 | 2751 */ |
2752 void | |
2753 changed() | |
2754 { | |
2755 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) | |
2756 /* The text of the preediting area is inserted, but this doesn't | |
2757 * mean a change of the buffer yet. That is delayed until the | |
2758 * text is committed. (this means preedit becomes empty) */ | |
2759 if (im_is_preediting() && !xim_changed_while_preediting) | |
2760 return; | |
2761 xim_changed_while_preediting = FALSE; | |
2762 #endif | |
2763 | |
2764 if (!curbuf->b_changed) | |
2765 { | |
2766 int save_msg_scroll = msg_scroll; | |
2767 | |
819 | 2768 /* Give a warning about changing a read-only file. This may also |
2769 * check-out the file, thus change "curbuf"! */ | |
7 | 2770 change_warning(0); |
819 | 2771 |
7 | 2772 /* Create a swap file if that is wanted. |
2773 * Don't do this for "nofile" and "nowrite" buffer types. */ | |
2774 if (curbuf->b_may_swap | |
2775 #ifdef FEAT_QUICKFIX | |
2776 && !bt_dontwrite(curbuf) | |
2777 #endif | |
2778 ) | |
2779 { | |
2780 ml_open_file(curbuf); | |
2781 | |
2782 /* The ml_open_file() can cause an ATTENTION message. | |
2783 * Wait two seconds, to make sure the user reads this unexpected | |
2784 * message. Since we could be anywhere, call wait_return() now, | |
2785 * and don't let the emsg() set msg_scroll. */ | |
2786 if (need_wait_return && emsg_silent == 0) | |
2787 { | |
2788 out_flush(); | |
2789 ui_delay(2000L, TRUE); | |
2790 wait_return(TRUE); | |
2791 msg_scroll = save_msg_scroll; | |
2792 } | |
2793 } | |
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
|
2794 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
|
2795 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2796 ++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
|
2797 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2798 |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2799 /* |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2800 * 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
|
2801 */ |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2802 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
|
2803 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
|
2804 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2805 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
|
2806 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
|
2807 #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
|
2808 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
|
2809 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
|
2810 #endif |
733f0dc510c3
Undo changes that are meant for the Vim 7.3 branch.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
2811 #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
|
2812 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
|
2813 #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
|
2814 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2815 |
265 | 2816 static void changedOneline __ARGS((buf_T *buf, linenr_T lnum)); |
2817 static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra)); | |
7 | 2818 static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra)); |
2819 | |
2820 /* | |
2821 * Changed bytes within a single line for the current buffer. | |
2822 * - marks the windows on this buffer to be redisplayed | |
2823 * - marks the buffer changed by calling changed() | |
2824 * - invalidates cached values | |
2189 | 2825 * Careful: may trigger autocommands that reload the buffer. |
7 | 2826 */ |
2827 void | |
2828 changed_bytes(lnum, col) | |
2829 linenr_T lnum; | |
2830 colnr_T col; | |
2831 { | |
265 | 2832 changedOneline(curbuf, lnum); |
7 | 2833 changed_common(lnum, col, lnum + 1, 0L); |
265 | 2834 |
2835 #ifdef FEAT_DIFF | |
2836 /* Diff highlighting in other diff windows may need to be updated too. */ | |
2837 if (curwin->w_p_diff) | |
2838 { | |
2839 win_T *wp; | |
2840 linenr_T wlnum; | |
2841 | |
2842 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2843 if (wp->w_p_diff && wp != curwin) | |
2844 { | |
2845 redraw_win_later(wp, VALID); | |
2846 wlnum = diff_lnum_win(lnum, wp); | |
2847 if (wlnum > 0) | |
2848 changedOneline(wp->w_buffer, wlnum); | |
2849 } | |
2850 } | |
2851 #endif | |
7 | 2852 } |
2853 | |
2854 static void | |
265 | 2855 changedOneline(buf, lnum) |
2856 buf_T *buf; | |
7 | 2857 linenr_T lnum; |
2858 { | |
265 | 2859 if (buf->b_mod_set) |
7 | 2860 { |
2861 /* find the maximum area that must be redisplayed */ | |
265 | 2862 if (lnum < buf->b_mod_top) |
2863 buf->b_mod_top = lnum; | |
2864 else if (lnum >= buf->b_mod_bot) | |
2865 buf->b_mod_bot = lnum + 1; | |
7 | 2866 } |
2867 else | |
2868 { | |
2869 /* set the area that must be redisplayed to one line */ | |
265 | 2870 buf->b_mod_set = TRUE; |
2871 buf->b_mod_top = lnum; | |
2872 buf->b_mod_bot = lnum + 1; | |
2873 buf->b_mod_xlines = 0; | |
7 | 2874 } |
2875 } | |
2876 | |
2877 /* | |
2878 * Appended "count" lines below line "lnum" in the current buffer. | |
2879 * Must be called AFTER the change and after mark_adjust(). | |
2880 * Takes care of marking the buffer to be redrawn and sets the changed flag. | |
2881 */ | |
2882 void | |
2883 appended_lines(lnum, count) | |
2884 linenr_T lnum; | |
2885 long count; | |
2886 { | |
2887 changed_lines(lnum + 1, 0, lnum + 1, count); | |
2888 } | |
2889 | |
2890 /* | |
2891 * Like appended_lines(), but adjust marks first. | |
2892 */ | |
2893 void | |
2894 appended_lines_mark(lnum, count) | |
2895 linenr_T lnum; | |
2896 long count; | |
2897 { | |
2898 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); | |
2899 changed_lines(lnum + 1, 0, lnum + 1, count); | |
2900 } | |
2901 | |
2902 /* | |
2903 * Deleted "count" lines at line "lnum" in the current buffer. | |
2904 * Must be called AFTER the change and after mark_adjust(). | |
2905 * Takes care of marking the buffer to be redrawn and sets the changed flag. | |
2906 */ | |
2907 void | |
2908 deleted_lines(lnum, count) | |
2909 linenr_T lnum; | |
2910 long count; | |
2911 { | |
2912 changed_lines(lnum, 0, lnum + count, -count); | |
2913 } | |
2914 | |
2915 /* | |
2916 * Like deleted_lines(), but adjust marks first. | |
1929 | 2917 * Make sure the cursor is on a valid line before calling, a GUI callback may |
2918 * be triggered to display the cursor. | |
7 | 2919 */ |
2920 void | |
2921 deleted_lines_mark(lnum, count) | |
2922 linenr_T lnum; | |
2923 long count; | |
2924 { | |
2925 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); | |
2926 changed_lines(lnum, 0, lnum + count, -count); | |
2927 } | |
2928 | |
2929 /* | |
2930 * Changed lines for the current buffer. | |
2931 * Must be called AFTER the change and after mark_adjust(). | |
2932 * - mark the buffer changed by calling changed() | |
2933 * - mark the windows on this buffer to be redisplayed | |
2934 * - invalidate cached values | |
2935 * "lnum" is the first line that needs displaying, "lnume" the first line | |
2936 * below the changed lines (BEFORE the change). | |
2937 * When only inserting lines, "lnum" and "lnume" are equal. | |
2938 * Takes care of calling changed() and updating b_mod_*. | |
2189 | 2939 * Careful: may trigger autocommands that reload the buffer. |
7 | 2940 */ |
2941 void | |
2942 changed_lines(lnum, col, lnume, xtra) | |
2943 linenr_T lnum; /* first line with change */ | |
2944 colnr_T col; /* column in first line with change */ | |
2945 linenr_T lnume; /* line below last changed line */ | |
2946 long xtra; /* number of extra lines (negative when deleting) */ | |
2947 { | |
265 | 2948 changed_lines_buf(curbuf, lnum, lnume, xtra); |
2949 | |
2950 #ifdef FEAT_DIFF | |
2951 if (xtra == 0 && curwin->w_p_diff) | |
2952 { | |
2953 /* When the number of lines doesn't change then mark_adjust() isn't | |
2954 * called and other diff buffers still need to be marked for | |
2955 * displaying. */ | |
2956 win_T *wp; | |
2957 linenr_T wlnum; | |
2958 | |
2959 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2960 if (wp->w_p_diff && wp != curwin) | |
2961 { | |
2962 redraw_win_later(wp, VALID); | |
2963 wlnum = diff_lnum_win(lnum, wp); | |
2964 if (wlnum > 0) | |
2965 changed_lines_buf(wp->w_buffer, wlnum, | |
2966 lnume - lnum + wlnum, 0L); | |
2967 } | |
2968 } | |
2969 #endif | |
2970 | |
2971 changed_common(lnum, col, lnume, xtra); | |
2972 } | |
2973 | |
2974 static void | |
2975 changed_lines_buf(buf, lnum, lnume, xtra) | |
2976 buf_T *buf; | |
2977 linenr_T lnum; /* first line with change */ | |
2978 linenr_T lnume; /* line below last changed line */ | |
2979 long xtra; /* number of extra lines (negative when deleting) */ | |
2980 { | |
2981 if (buf->b_mod_set) | |
7 | 2982 { |
2983 /* find the maximum area that must be redisplayed */ | |
265 | 2984 if (lnum < buf->b_mod_top) |
2985 buf->b_mod_top = lnum; | |
2986 if (lnum < buf->b_mod_bot) | |
7 | 2987 { |
2988 /* adjust old bot position for xtra lines */ | |
265 | 2989 buf->b_mod_bot += xtra; |
2990 if (buf->b_mod_bot < lnum) | |
2991 buf->b_mod_bot = lnum; | |
7 | 2992 } |
265 | 2993 if (lnume + xtra > buf->b_mod_bot) |
2994 buf->b_mod_bot = lnume + xtra; | |
2995 buf->b_mod_xlines += xtra; | |
7 | 2996 } |
2997 else | |
2998 { | |
2999 /* set the area that must be redisplayed */ | |
265 | 3000 buf->b_mod_set = TRUE; |
3001 buf->b_mod_top = lnum; | |
3002 buf->b_mod_bot = lnume + xtra; | |
3003 buf->b_mod_xlines = xtra; | |
3004 } | |
7 | 3005 } |
3006 | |
2189 | 3007 /* |
3008 * Common code for when a change is was made. | |
3009 * See changed_lines() for the arguments. | |
3010 * Careful: may trigger autocommands that reload the buffer. | |
3011 */ | |
7 | 3012 static void |
3013 changed_common(lnum, col, lnume, xtra) | |
3014 linenr_T lnum; | |
3015 colnr_T col; | |
3016 linenr_T lnume; | |
3017 long xtra; | |
3018 { | |
3019 win_T *wp; | |
1863 | 3020 #ifdef FEAT_WINDOWS |
3021 tabpage_T *tp; | |
3022 #endif | |
7 | 3023 int i; |
3024 #ifdef FEAT_JUMPLIST | |
3025 int cols; | |
3026 pos_T *p; | |
3027 int add; | |
3028 #endif | |
3029 | |
3030 /* mark the buffer as modified */ | |
3031 changed(); | |
3032 | |
3033 /* set the '. mark */ | |
3034 if (!cmdmod.keepjumps) | |
3035 { | |
3036 curbuf->b_last_change.lnum = lnum; | |
3037 curbuf->b_last_change.col = col; | |
3038 | |
3039 #ifdef FEAT_JUMPLIST | |
3040 /* Create a new entry if a new undo-able change was started or we | |
3041 * don't have an entry yet. */ | |
3042 if (curbuf->b_new_change || curbuf->b_changelistlen == 0) | |
3043 { | |
3044 if (curbuf->b_changelistlen == 0) | |
3045 add = TRUE; | |
3046 else | |
3047 { | |
3048 /* Don't create a new entry when the line number is the same | |
3049 * as the last one and the column is not too far away. Avoids | |
3050 * creating many entries for typing "xxxxx". */ | |
3051 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; | |
3052 if (p->lnum != lnum) | |
3053 add = TRUE; | |
3054 else | |
3055 { | |
3056 cols = comp_textwidth(FALSE); | |
3057 if (cols == 0) | |
3058 cols = 79; | |
3059 add = (p->col + cols < col || col + cols < p->col); | |
3060 } | |
3061 } | |
3062 if (add) | |
3063 { | |
3064 /* This is the first of a new sequence of undo-able changes | |
3065 * and it's at some distance of the last change. Use a new | |
3066 * position in the changelist. */ | |
3067 curbuf->b_new_change = FALSE; | |
3068 | |
3069 if (curbuf->b_changelistlen == JUMPLISTSIZE) | |
3070 { | |
3071 /* changelist is full: remove oldest entry */ | |
3072 curbuf->b_changelistlen = JUMPLISTSIZE - 1; | |
3073 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, | |
3074 sizeof(pos_T) * (JUMPLISTSIZE - 1)); | |
1863 | 3075 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 3076 { |
3077 /* Correct position in changelist for other windows on | |
3078 * this buffer. */ | |
3079 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) | |
3080 --wp->w_changelistidx; | |
3081 } | |
3082 } | |
1863 | 3083 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 3084 { |
3085 /* For other windows, if the position in the changelist is | |
3086 * at the end it stays at the end. */ | |
3087 if (wp->w_buffer == curbuf | |
3088 && wp->w_changelistidx == curbuf->b_changelistlen) | |
3089 ++wp->w_changelistidx; | |
3090 } | |
3091 ++curbuf->b_changelistlen; | |
3092 } | |
3093 } | |
3094 curbuf->b_changelist[curbuf->b_changelistlen - 1] = | |
3095 curbuf->b_last_change; | |
3096 /* The current window is always after the last change, so that "g," | |
3097 * takes you back to it. */ | |
3098 curwin->w_changelistidx = curbuf->b_changelistlen; | |
3099 #endif | |
3100 } | |
3101 | |
1863 | 3102 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 3103 { |
3104 if (wp->w_buffer == curbuf) | |
3105 { | |
3106 /* Mark this window to be redrawn later. */ | |
3107 if (wp->w_redr_type < VALID) | |
3108 wp->w_redr_type = VALID; | |
3109 | |
3110 /* Check if a change in the buffer has invalidated the cached | |
3111 * values for the cursor. */ | |
3112 #ifdef FEAT_FOLDING | |
3113 /* | |
3114 * Update the folds for this window. Can't postpone this, because | |
3115 * a following operator might work on the whole fold: ">>dd". | |
3116 */ | |
3117 foldUpdate(wp, lnum, lnume + xtra - 1); | |
3118 | |
3119 /* The change may cause lines above or below the change to become | |
3120 * included in a fold. Set lnum/lnume to the first/last line that | |
3121 * might be displayed differently. | |
3122 * Set w_cline_folded here as an efficient way to update it when | |
3123 * inserting lines just above a closed fold. */ | |
3124 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); | |
3125 if (wp->w_cursor.lnum == lnum) | |
3126 wp->w_cline_folded = i; | |
3127 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); | |
3128 if (wp->w_cursor.lnum == lnume) | |
3129 wp->w_cline_folded = i; | |
3130 | |
3131 /* If the changed line is in a range of previously folded lines, | |
3132 * compare with the first line in that range. */ | |
3133 if (wp->w_cursor.lnum <= lnum) | |
3134 { | |
3135 i = find_wl_entry(wp, lnum); | |
3136 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) | |
3137 changed_line_abv_curs_win(wp); | |
3138 } | |
3139 #endif | |
3140 | |
3141 if (wp->w_cursor.lnum > lnum) | |
3142 changed_line_abv_curs_win(wp); | |
3143 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) | |
3144 changed_cline_bef_curs_win(wp); | |
3145 if (wp->w_botline >= lnum) | |
3146 { | |
3147 /* Assume that botline doesn't change (inserted lines make | |
3148 * other lines scroll down below botline). */ | |
3149 approximate_botline_win(wp); | |
3150 } | |
3151 | |
3152 /* Check if any w_lines[] entries have become invalid. | |
3153 * For entries below the change: Correct the lnums for | |
3154 * inserted/deleted lines. Makes it possible to stop displaying | |
3155 * after the change. */ | |
3156 for (i = 0; i < wp->w_lines_valid; ++i) | |
3157 if (wp->w_lines[i].wl_valid) | |
3158 { | |
3159 if (wp->w_lines[i].wl_lnum >= lnum) | |
3160 { | |
3161 if (wp->w_lines[i].wl_lnum < lnume) | |
3162 { | |
3163 /* line included in change */ | |
3164 wp->w_lines[i].wl_valid = FALSE; | |
3165 } | |
3166 else if (xtra != 0) | |
3167 { | |
3168 /* line below change */ | |
3169 wp->w_lines[i].wl_lnum += xtra; | |
3170 #ifdef FEAT_FOLDING | |
3171 wp->w_lines[i].wl_lastlnum += xtra; | |
3172 #endif | |
3173 } | |
3174 } | |
3175 #ifdef FEAT_FOLDING | |
3176 else if (wp->w_lines[i].wl_lastlnum >= lnum) | |
3177 { | |
3178 /* change somewhere inside this range of folded lines, | |
3179 * may need to be redrawn */ | |
3180 wp->w_lines[i].wl_valid = FALSE; | |
3181 } | |
3182 #endif | |
3183 } | |
1987 | 3184 |
3185 #ifdef FEAT_FOLDING | |
3186 /* Take care of side effects for setting w_topline when folds have | |
3187 * changed. Esp. when the buffer was changed in another window. */ | |
3188 if (hasAnyFolding(wp)) | |
3189 set_topline(wp, wp->w_topline); | |
3190 #endif | |
5885 | 3191 /* relative numbering may require updating more */ |
3192 if (wp->w_p_rnu) | |
3193 redraw_win_later(wp, SOME_VALID); | |
7 | 3194 } |
3195 } | |
3196 | |
3197 /* Call update_screen() later, which checks out what needs to be redrawn, | |
3198 * since it notices b_mod_set and then uses b_mod_*. */ | |
3199 if (must_redraw < VALID) | |
3200 must_redraw = VALID; | |
694 | 3201 |
3202 #ifdef FEAT_AUTOCMD | |
3203 /* when the cursor line is changed always trigger CursorMoved */ | |
1010 | 3204 if (lnum <= curwin->w_cursor.lnum |
3205 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) | |
694 | 3206 last_cursormoved.lnum = 0; |
3207 #endif | |
7 | 3208 } |
3209 | |
3210 /* | |
3211 * unchanged() is called when the changed flag must be reset for buffer 'buf' | |
3212 */ | |
3213 void | |
3214 unchanged(buf, ff) | |
3215 buf_T *buf; | |
3216 int ff; /* also reset 'fileformat' */ | |
3217 { | |
2685 | 3218 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
7 | 3219 { |
3220 buf->b_changed = 0; | |
39 | 3221 ml_setflags(buf); |
7 | 3222 if (ff) |
3223 save_file_ff(buf); | |
3224 #ifdef FEAT_WINDOWS | |
3225 check_status(buf); | |
673 | 3226 redraw_tabline = TRUE; |
7 | 3227 #endif |
3228 #ifdef FEAT_TITLE | |
3229 need_maketitle = TRUE; /* set window title later */ | |
3230 #endif | |
3231 } | |
3232 ++buf->b_changedtick; | |
3233 #ifdef FEAT_NETBEANS_INTG | |
3234 netbeans_unmodified(buf); | |
3235 #endif | |
3236 } | |
3237 | |
3238 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
3239 /* | |
3240 * check_status: called when the status bars for the buffer 'buf' | |
3241 * need to be updated | |
3242 */ | |
3243 void | |
3244 check_status(buf) | |
3245 buf_T *buf; | |
3246 { | |
3247 win_T *wp; | |
3248 | |
3249 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
3250 if (wp->w_buffer == buf && wp->w_status_height) | |
3251 { | |
3252 wp->w_redr_status = TRUE; | |
3253 if (must_redraw < VALID) | |
3254 must_redraw = VALID; | |
3255 } | |
3256 } | |
3257 #endif | |
3258 | |
3259 /* | |
3260 * If the file is readonly, give a warning message with the first change. | |
3261 * Don't do this for autocommands. | |
3262 * Don't use emsg(), because it flushes the macro buffer. | |
548 | 3263 * If we have undone all changes b_changed will be FALSE, but "b_did_warn" |
7 | 3264 * will be TRUE. |
2189 | 3265 * Careful: may trigger autocommands that reload the buffer. |
7 | 3266 */ |
3267 void | |
3268 change_warning(col) | |
3269 int col; /* column for message; non-zero when in insert | |
3270 mode and 'showmode' is on */ | |
3271 { | |
1848 | 3272 static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
3273 | |
7 | 3274 if (curbuf->b_did_warn == FALSE |
3275 && curbufIsChanged() == 0 | |
3276 #ifdef FEAT_AUTOCMD | |
3277 && !autocmd_busy | |
3278 #endif | |
3279 && curbuf->b_p_ro) | |
3280 { | |
3281 #ifdef FEAT_AUTOCMD | |
819 | 3282 ++curbuf_lock; |
7 | 3283 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
819 | 3284 --curbuf_lock; |
7 | 3285 if (!curbuf->b_p_ro) |
3286 return; | |
3287 #endif | |
3288 /* | |
3289 * Do what msg() does, but with a column offset if the warning should | |
3290 * be after the mode message. | |
3291 */ | |
3292 msg_start(); | |
3293 if (msg_row == Rows - 1) | |
3294 msg_col = col; | |
16 | 3295 msg_source(hl_attr(HLF_W)); |
1848 | 3296 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST); |
3297 #ifdef FEAT_EVAL | |
3298 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); | |
3299 #endif | |
7 | 3300 msg_clr_eos(); |
3301 (void)msg_end(); | |
3302 if (msg_silent == 0 && !silent_mode) | |
3303 { | |
3304 out_flush(); | |
3305 ui_delay(1000L, TRUE); /* give the user time to think about it */ | |
3306 } | |
3307 curbuf->b_did_warn = TRUE; | |
3308 redraw_cmdline = FALSE; /* don't redraw and erase the message */ | |
3309 if (msg_row < Rows - 1) | |
3310 showmode(); | |
3311 } | |
3312 } | |
3313 | |
3314 /* | |
3315 * Ask for a reply from the user, a 'y' or a 'n'. | |
3316 * No other characters are accepted, the message is repeated until a valid | |
3317 * reply is entered or CTRL-C is hit. | |
3318 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters | |
3319 * from any buffers but directly from the user. | |
3320 * | |
3321 * return the 'y' or 'n' | |
3322 */ | |
3323 int | |
3324 ask_yesno(str, direct) | |
3325 char_u *str; | |
3326 int direct; | |
3327 { | |
3328 int r = ' '; | |
3329 int save_State = State; | |
3330 | |
3331 if (exiting) /* put terminal in raw mode for this question */ | |
3332 settmode(TMODE_RAW); | |
3333 ++no_wait_return; | |
3334 #ifdef USE_ON_FLY_SCROLL | |
3335 dont_scroll = TRUE; /* disallow scrolling here */ | |
3336 #endif | |
3337 State = CONFIRM; /* mouse behaves like with :confirm */ | |
3338 #ifdef FEAT_MOUSE | |
3339 setmouse(); /* disables mouse for xterm */ | |
3340 #endif | |
3341 ++no_mapping; | |
3342 ++allow_keys; /* no mapping here, but recognize keys */ | |
3343 | |
3344 while (r != 'y' && r != 'n') | |
3345 { | |
3346 /* same highlighting as for wait_return */ | |
3347 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str); | |
3348 if (direct) | |
3349 r = get_keystroke(); | |
3350 else | |
1474 | 3351 r = plain_vgetc(); |
7 | 3352 if (r == Ctrl_C || r == ESC) |
3353 r = 'n'; | |
3354 msg_putchar(r); /* show what you typed */ | |
3355 out_flush(); | |
3356 } | |
3357 --no_wait_return; | |
3358 State = save_State; | |
3359 #ifdef FEAT_MOUSE | |
3360 setmouse(); | |
3361 #endif | |
3362 --no_mapping; | |
3363 --allow_keys; | |
3364 | |
3365 return r; | |
3366 } | |
3367 | |
4221 | 3368 #if defined(FEAT_MOUSE) || defined(PROTO) |
3369 /* | |
3370 * Return TRUE if "c" is a mouse key. | |
3371 */ | |
3372 int | |
3373 is_mouse_key(c) | |
3374 int c; | |
3375 { | |
3376 return c == K_LEFTMOUSE | |
3377 || c == K_LEFTMOUSE_NM | |
3378 || c == K_LEFTDRAG | |
3379 || c == K_LEFTRELEASE | |
3380 || c == K_LEFTRELEASE_NM | |
3381 || c == K_MIDDLEMOUSE | |
3382 || c == K_MIDDLEDRAG | |
3383 || c == K_MIDDLERELEASE | |
3384 || c == K_RIGHTMOUSE | |
3385 || c == K_RIGHTDRAG | |
3386 || c == K_RIGHTRELEASE | |
3387 || c == K_MOUSEDOWN | |
3388 || c == K_MOUSEUP | |
3389 || c == K_MOUSELEFT | |
3390 || c == K_MOUSERIGHT | |
3391 || c == K_X1MOUSE | |
3392 || c == K_X1DRAG | |
3393 || c == K_X1RELEASE | |
3394 || c == K_X2MOUSE | |
3395 || c == K_X2DRAG | |
3396 || c == K_X2RELEASE; | |
3397 } | |
3398 #endif | |
3399 | |
7 | 3400 /* |
3401 * Get a key stroke directly from the user. | |
3402 * Ignores mouse clicks and scrollbar events, except a click for the left | |
3403 * button (used at the more prompt). | |
3404 * Doesn't use vgetc(), because it syncs undo and eats mapped characters. | |
3405 * Disadvantage: typeahead is ignored. | |
3406 * Translates the interrupt character for unix to ESC. | |
3407 */ | |
3408 int | |
3409 get_keystroke() | |
3410 { | |
3328 | 3411 char_u *buf = NULL; |
3412 int buflen = 150; | |
3413 int maxlen; | |
7 | 3414 int len = 0; |
3415 int n; | |
3416 int save_mapped_ctrl_c = mapped_ctrl_c; | |
964 | 3417 int waited = 0; |
7 | 3418 |
3419 mapped_ctrl_c = FALSE; /* mappings are not used here */ | |
3420 for (;;) | |
3421 { | |
3422 cursor_on(); | |
3423 out_flush(); | |
3424 | |
3328 | 3425 /* Leave some room for check_termcode() to insert a key code into (max |
3426 * 5 chars plus NUL). And fix_input_buffer() can triple the number of | |
3427 * bytes. */ | |
3428 maxlen = (buflen - 6 - len) / 3; | |
3429 if (buf == NULL) | |
3430 buf = alloc(buflen); | |
3431 else if (maxlen < 10) | |
3432 { | |
3596 | 3433 /* Need some more space. This might happen when receiving a long |
3328 | 3434 * escape sequence. */ |
3435 buflen += 100; | |
3436 buf = vim_realloc(buf, buflen); | |
3437 maxlen = (buflen - 6 - len) / 3; | |
3438 } | |
3439 if (buf == NULL) | |
3440 { | |
3441 do_outofmem_msg((long_u)buflen); | |
3442 return ESC; /* panic! */ | |
3443 } | |
3444 | |
7 | 3445 /* First time: blocking wait. Second time: wait up to 100ms for a |
3328 | 3446 * terminal code to complete. */ |
3447 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); | |
7 | 3448 if (n > 0) |
3449 { | |
3450 /* Replace zero and CSI by a special key code. */ | |
3451 n = fix_input_buffer(buf + len, n, FALSE); | |
3452 len += n; | |
964 | 3453 waited = 0; |
3454 } | |
3455 else if (len > 0) | |
3456 ++waited; /* keep track of the waiting time */ | |
3457 | |
3458 /* Incomplete termcode and not timed out yet: get more characters */ | |
3328 | 3459 if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
964 | 3460 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
7 | 3461 continue; |
964 | 3462 |
2672 | 3463 if (n == KEYLEN_REMOVED) /* key code removed */ |
2721 | 3464 { |
2723 | 3465 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
2721 | 3466 { |
3467 /* Redrawing was postponed, do it now. */ | |
3468 update_screen(0); | |
3469 setcursor(); /* put cursor back where it belongs */ | |
3470 } | |
2672 | 3471 continue; |
2721 | 3472 } |
2672 | 3473 if (n > 0) /* found a termcode: adjust length */ |
7 | 3474 len = n; |
2672 | 3475 if (len == 0) /* nothing typed yet */ |
7 | 3476 continue; |
3477 | |
3478 /* Handle modifier and/or special key code. */ | |
3479 n = buf[0]; | |
3480 if (n == K_SPECIAL) | |
3481 { | |
3482 n = TO_SPECIAL(buf[1], buf[2]); | |
3483 if (buf[1] == KS_MODIFIER | |
3484 || n == K_IGNORE | |
4225 | 3485 #ifdef FEAT_MOUSE |
4221 | 3486 || (is_mouse_key(n) && n != K_LEFTMOUSE) |
4225 | 3487 #endif |
4221 | 3488 #ifdef FEAT_GUI |
7 | 3489 || n == K_VER_SCROLLBAR |
3490 || n == K_HOR_SCROLLBAR | |
3491 #endif | |
3492 ) | |
3493 { | |
3494 if (buf[1] == KS_MODIFIER) | |
3495 mod_mask = buf[2]; | |
3496 len -= 3; | |
3497 if (len > 0) | |
3498 mch_memmove(buf, buf + 3, (size_t)len); | |
3499 continue; | |
3500 } | |
828 | 3501 break; |
7 | 3502 } |
3503 #ifdef FEAT_MBYTE | |
3504 if (has_mbyte) | |
3505 { | |
3506 if (MB_BYTE2LEN(n) > len) | |
3507 continue; /* more bytes to get */ | |
3328 | 3508 buf[len >= buflen ? buflen - 1 : len] = NUL; |
7 | 3509 n = (*mb_ptr2char)(buf); |
3510 } | |
3511 #endif | |
3512 #ifdef UNIX | |
3513 if (n == intr_char) | |
3514 n = ESC; | |
3515 #endif | |
3516 break; | |
3517 } | |
3328 | 3518 vim_free(buf); |
7 | 3519 |
3520 mapped_ctrl_c = save_mapped_ctrl_c; | |
3521 return n; | |
3522 } | |
3523 | |
3524 /* | |
374 | 3525 * Get a number from the user. |
3526 * When "mouse_used" is not NULL allow using the mouse. | |
7 | 3527 */ |
3528 int | |
374 | 3529 get_number(colon, mouse_used) |
3530 int colon; /* allow colon to abort */ | |
3531 int *mouse_used; | |
7 | 3532 { |
3533 int n = 0; | |
3534 int c; | |
810 | 3535 int typed = 0; |
7 | 3536 |
374 | 3537 if (mouse_used != NULL) |
3538 *mouse_used = FALSE; | |
3539 | |
7 | 3540 /* When not printing messages, the user won't know what to type, return a |
3541 * zero (as if CR was hit). */ | |
3542 if (msg_silent != 0) | |
3543 return 0; | |
3544 | |
3545 #ifdef USE_ON_FLY_SCROLL | |
3546 dont_scroll = TRUE; /* disallow scrolling here */ | |
3547 #endif | |
3548 ++no_mapping; | |
3549 ++allow_keys; /* no mapping here, but recognize keys */ | |
3550 for (;;) | |
3551 { | |
3552 windgoto(msg_row, msg_col); | |
3553 c = safe_vgetc(); | |
3554 if (VIM_ISDIGIT(c)) | |
3555 { | |
3556 n = n * 10 + c - '0'; | |
3557 msg_putchar(c); | |
810 | 3558 ++typed; |
7 | 3559 } |
3560 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) | |
3561 { | |
810 | 3562 if (typed > 0) |
3563 { | |
3564 MSG_PUTS("\b \b"); | |
3565 --typed; | |
3566 } | |
7 | 3567 n /= 10; |
3568 } | |
374 | 3569 #ifdef FEAT_MOUSE |
3570 else if (mouse_used != NULL && c == K_LEFTMOUSE) | |
3571 { | |
3572 *mouse_used = TRUE; | |
3573 n = mouse_row + 1; | |
3574 break; | |
3575 } | |
3576 #endif | |
7 | 3577 else if (n == 0 && c == ':' && colon) |
3578 { | |
3579 stuffcharReadbuff(':'); | |
3580 if (!exmode_active) | |
3581 cmdline_row = msg_row; | |
3582 skip_redraw = TRUE; /* skip redraw once */ | |
3583 do_redraw = FALSE; | |
3584 break; | |
3585 } | |
3586 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) | |
3587 break; | |
3588 } | |
3589 --no_mapping; | |
3590 --allow_keys; | |
3591 return n; | |
3592 } | |
3593 | |
323 | 3594 /* |
3595 * Ask the user to enter a number. | |
374 | 3596 * When "mouse_used" is not NULL allow using the mouse and in that case return |
3597 * the line number. | |
323 | 3598 */ |
3599 int | |
374 | 3600 prompt_for_number(mouse_used) |
3601 int *mouse_used; | |
323 | 3602 { |
3603 int i; | |
344 | 3604 int save_cmdline_row; |
3605 int save_State; | |
323 | 3606 |
3607 /* When using ":silent" assume that <CR> was entered. */ | |
375 | 3608 if (mouse_used != NULL) |
1733 | 3609 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
375 | 3610 else |
1733 | 3611 MSG_PUTS(_("Type number and <Enter> (empty cancels): ")); |
344 | 3612 |
957 | 3613 /* Set the state such that text can be selected/copied/pasted and we still |
3614 * get mouse events. */ | |
344 | 3615 save_cmdline_row = cmdline_row; |
957 | 3616 cmdline_row = 0; |
344 | 3617 save_State = State; |
957 | 3618 State = CMDLINE; |
374 | 3619 |
3620 i = get_number(TRUE, mouse_used); | |
3621 if (KeyTyped) | |
3622 { | |
3623 /* don't call wait_return() now */ | |
3624 /* msg_putchar('\n'); */ | |
323 | 3625 cmdline_row = msg_row - 1; |
3626 need_wait_return = FALSE; | |
3627 msg_didany = FALSE; | |
1938 | 3628 msg_didout = FALSE; |
323 | 3629 } |
344 | 3630 else |
3631 cmdline_row = save_cmdline_row; | |
3632 State = save_State; | |
3633 | |
323 | 3634 return i; |
3635 } | |
3636 | |
7 | 3637 void |
3638 msgmore(n) | |
3639 long n; | |
3640 { | |
3641 long pn; | |
3642 | |
3643 if (global_busy /* no messages now, wait until global is finished */ | |
3644 || !messaging()) /* 'lazyredraw' set, don't do messages now */ | |
3645 return; | |
3646 | |
135 | 3647 /* We don't want to overwrite another important message, but do overwrite |
3648 * a previous "more lines" or "fewer lines" message, so that "5dd" and | |
3649 * then "put" reports the last action. */ | |
3650 if (keep_msg != NULL && !keep_msg_more) | |
3651 return; | |
3652 | |
7 | 3653 if (n > 0) |
3654 pn = n; | |
3655 else | |
3656 pn = -n; | |
3657 | |
3658 if (pn > p_report) | |
3659 { | |
3660 if (pn == 1) | |
3661 { | |
3662 if (n > 0) | |
2768 | 3663 vim_strncpy(msg_buf, (char_u *)_("1 more line"), |
3664 MSG_BUF_LEN - 1); | |
7 | 3665 else |
2768 | 3666 vim_strncpy(msg_buf, (char_u *)_("1 line less"), |
3667 MSG_BUF_LEN - 1); | |
7 | 3668 } |
3669 else | |
3670 { | |
3671 if (n > 0) | |
2768 | 3672 vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
3673 _("%ld more lines"), pn); | |
7 | 3674 else |
2768 | 3675 vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
3676 _("%ld fewer lines"), pn); | |
7 | 3677 } |
3678 if (got_int) | |
2768 | 3679 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN); |
7 | 3680 if (msg(msg_buf)) |
3681 { | |
680 | 3682 set_keep_msg(msg_buf, 0); |
135 | 3683 keep_msg_more = TRUE; |
7 | 3684 } |
3685 } | |
3686 } | |
3687 | |
3688 /* | |
3689 * flush map and typeahead buffers and give a warning for an error | |
3690 */ | |
3691 void | |
3692 beep_flush() | |
3693 { | |
3694 if (emsg_silent == 0) | |
3695 { | |
3696 flush_buffers(FALSE); | |
3697 vim_beep(); | |
3698 } | |
3699 } | |
3700 | |
3701 /* | |
3702 * give a warning for an error | |
3703 */ | |
3704 void | |
3705 vim_beep() | |
3706 { | |
3707 if (emsg_silent == 0) | |
3708 { | |
3709 if (p_vb | |
3710 #ifdef FEAT_GUI | |
3711 /* While the GUI is starting up the termcap is set for the GUI | |
3712 * but the output still goes to a terminal. */ | |
3713 && !(gui.in_use && gui.starting) | |
3714 #endif | |
3715 ) | |
3716 { | |
3717 out_str(T_VB); | |
3718 } | |
3719 else | |
3720 { | |
3721 #ifdef MSDOS | |
3722 /* | |
3723 * The number of beeps outputted is reduced to avoid having to wait | |
3724 * for all the beeps to finish. This is only a problem on systems | |
3725 * where the beeps don't overlap. | |
3726 */ | |
3727 if (beep_count == 0 || beep_count == 10) | |
3728 { | |
3729 out_char(BELL); | |
3730 beep_count = 1; | |
3731 } | |
3732 else | |
3733 ++beep_count; | |
3734 #else | |
3735 out_char(BELL); | |
3736 #endif | |
3737 } | |
169 | 3738 |
3739 /* When 'verbose' is set and we are sourcing a script or executing a | |
3740 * function give the user a hint where the beep comes from. */ | |
3741 if (vim_strchr(p_debug, 'e') != NULL) | |
3742 { | |
3743 msg_source(hl_attr(HLF_W)); | |
3744 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W)); | |
3745 } | |
7 | 3746 } |
3747 } | |
3748 | |
3749 /* | |
3750 * To get the "real" home directory: | |
3751 * - get value of $HOME | |
3752 * For Unix: | |
3753 * - go to that directory | |
3754 * - do mch_dirname() to get the real name of that directory. | |
3755 * This also works with mounts and links. | |
3756 * Don't do this for MS-DOS, it will change the "current dir" for a drive. | |
3757 */ | |
3758 static char_u *homedir = NULL; | |
3759 | |
3760 void | |
3761 init_homedir() | |
3762 { | |
3763 char_u *var; | |
3764 | |
170 | 3765 /* In case we are called a second time (when 'encoding' changes). */ |
3766 vim_free(homedir); | |
3767 homedir = NULL; | |
3768 | |
7 | 3769 #ifdef VMS |
3770 var = mch_getenv((char_u *)"SYS$LOGIN"); | |
3771 #else | |
3772 var = mch_getenv((char_u *)"HOME"); | |
3773 #endif | |
3774 | |
3775 if (var != NULL && *var == NUL) /* empty is same as not set */ | |
3776 var = NULL; | |
3777 | |
3778 #ifdef WIN3264 | |
3779 /* | |
3780 * Weird but true: $HOME may contain an indirect reference to another | |
3781 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set | |
3782 * when $HOME is being set. | |
3783 */ | |
3784 if (var != NULL && *var == '%') | |
3785 { | |
3786 char_u *p; | |
3787 char_u *exp; | |
3788 | |
3789 p = vim_strchr(var + 1, '%'); | |
3790 if (p != NULL) | |
3791 { | |
419 | 3792 vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
7 | 3793 exp = mch_getenv(NameBuff); |
3794 if (exp != NULL && *exp != NUL | |
3795 && STRLEN(exp) + STRLEN(p) < MAXPATHL) | |
3796 { | |
274 | 3797 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
7 | 3798 var = NameBuff; |
3799 /* Also set $HOME, it's needed for _viminfo. */ | |
3800 vim_setenv((char_u *)"HOME", NameBuff); | |
3801 } | |
3802 } | |
3803 } | |
3804 | |
3805 /* | |
3806 * Typically, $HOME is not defined on Windows, unless the user has | |
3807 * specifically defined it for Vim's sake. However, on Windows NT | |
3808 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for | |
3809 * each user. Try constructing $HOME from these. | |
3810 */ | |
3811 if (var == NULL) | |
3812 { | |
3813 char_u *homedrive, *homepath; | |
3814 | |
3815 homedrive = mch_getenv((char_u *)"HOMEDRIVE"); | |
3816 homepath = mch_getenv((char_u *)"HOMEPATH"); | |
2030
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3817 if (homepath == NULL || *homepath == NUL) |
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3818 homepath = "\\"; |
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3819 if (homedrive != NULL |
7 | 3820 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
3821 { | |
3822 sprintf((char *)NameBuff, "%s%s", homedrive, homepath); | |
3823 if (NameBuff[0] != NUL) | |
3824 { | |
3825 var = NameBuff; | |
3826 /* Also set $HOME, it's needed for _viminfo. */ | |
3827 vim_setenv((char_u *)"HOME", NameBuff); | |
3828 } | |
3829 } | |
3830 } | |
170 | 3831 |
3832 # if defined(FEAT_MBYTE) | |
3833 if (enc_utf8 && var != NULL) | |
3834 { | |
3835 int len; | |
2786 | 3836 char_u *pp = NULL; |
170 | 3837 |
3838 /* Convert from active codepage to UTF-8. Other conversions are | |
3839 * not done, because they would fail for non-ASCII characters. */ | |
835 | 3840 acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
170 | 3841 if (pp != NULL) |
3842 { | |
3843 homedir = pp; | |
3844 return; | |
3845 } | |
3846 } | |
3847 # endif | |
7 | 3848 #endif |
3849 | |
3850 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) | |
3851 /* | |
3852 * Default home dir is C:/ | |
3853 * Best assumption we can make in such a situation. | |
3854 */ | |
3855 if (var == NULL) | |
3856 var = "C:/"; | |
3857 #endif | |
3858 if (var != NULL) | |
3859 { | |
3860 #ifdef UNIX | |
3861 /* | |
3862 * Change to the directory and get the actual path. This resolves | |
3863 * links. Don't do it when we can't return. | |
3864 */ | |
3865 if (mch_dirname(NameBuff, MAXPATHL) == OK | |
3866 && mch_chdir((char *)NameBuff) == 0) | |
3867 { | |
3868 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) | |
3869 var = IObuff; | |
3870 if (mch_chdir((char *)NameBuff) != 0) | |
3871 EMSG(_(e_prev_dir)); | |
3872 } | |
3873 #endif | |
3874 homedir = vim_strsave(var); | |
3875 } | |
3876 } | |
3877 | |
359 | 3878 #if defined(EXITFREE) || defined(PROTO) |
3879 void | |
3880 free_homedir() | |
3881 { | |
3882 vim_free(homedir); | |
3883 } | |
3744 | 3884 |
3885 # ifdef FEAT_CMDL_COMPL | |
3886 void | |
3887 free_users() | |
3888 { | |
3889 ga_clear_strings(&ga_users); | |
3890 } | |
3891 # endif | |
359 | 3892 #endif |
3893 | |
7 | 3894 /* |
1408 | 3895 * Call expand_env() and store the result in an allocated string. |
3896 * This is not very memory efficient, this expects the result to be freed | |
3897 * again soon. | |
3898 */ | |
3899 char_u * | |
3900 expand_env_save(src) | |
3901 char_u *src; | |
3902 { | |
3903 return expand_env_save_opt(src, FALSE); | |
3904 } | |
3905 | |
3906 /* | |
3907 * Idem, but when "one" is TRUE handle the string as one file name, only | |
3908 * expand "~" at the start. | |
3909 */ | |
3910 char_u * | |
3911 expand_env_save_opt(src, one) | |
3912 char_u *src; | |
3913 int one; | |
3914 { | |
3915 char_u *p; | |
3916 | |
3917 p = alloc(MAXPATHL); | |
3918 if (p != NULL) | |
3919 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); | |
3920 return p; | |
3921 } | |
3922 | |
3923 /* | |
7 | 3924 * Expand environment variable with path name. |
3925 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. | |
1408 | 3926 * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
7 | 3927 * If anything fails no expansion is done and dst equals src. |
3928 */ | |
3929 void | |
3930 expand_env(src, dst, dstlen) | |
3931 char_u *src; /* input string e.g. "$HOME/vim.hlp" */ | |
3932 char_u *dst; /* where to put the result */ | |
3933 int dstlen; /* maximum length of the result */ | |
3934 { | |
1408 | 3935 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
7 | 3936 } |
3937 | |
3938 void | |
1408 | 3939 expand_env_esc(srcp, dst, dstlen, esc, one, startstr) |
374 | 3940 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */ |
7 | 3941 char_u *dst; /* where to put the result */ |
3942 int dstlen; /* maximum length of the result */ | |
3943 int esc; /* escape spaces in expanded variables */ | |
1408 | 3944 int one; /* "srcp" is one file name */ |
374 | 3945 char_u *startstr; /* start again after this (can be NULL) */ |
3946 { | |
3947 char_u *src; | |
7 | 3948 char_u *tail; |
3949 int c; | |
3950 char_u *var; | |
3951 int copy_char; | |
3952 int mustfree; /* var was allocated, need to free it later */ | |
3953 int at_start = TRUE; /* at start of a name */ | |
374 | 3954 int startstr_len = 0; |
3955 | |
3956 if (startstr != NULL) | |
835 | 3957 startstr_len = (int)STRLEN(startstr); |
374 | 3958 |
3959 src = skipwhite(srcp); | |
7 | 3960 --dstlen; /* leave one char space for "\," */ |
3961 while (*src && dstlen > 0) | |
3962 { | |
3963 copy_char = TRUE; | |
22 | 3964 if ((*src == '$' |
3965 #ifdef VMS | |
3966 && at_start | |
3967 #endif | |
3968 ) | |
7 | 3969 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
3970 || *src == '%' | |
3971 #endif | |
3972 || (*src == '~' && at_start)) | |
3973 { | |
3974 mustfree = FALSE; | |
3975 | |
3976 /* | |
3977 * The variable name is copied into dst temporarily, because it may | |
3978 * be a string in read-only memory and a NUL needs to be appended. | |
3979 */ | |
3980 if (*src != '~') /* environment var */ | |
3981 { | |
3982 tail = src + 1; | |
3983 var = dst; | |
3984 c = dstlen - 1; | |
3985 | |
3986 #ifdef UNIX | |
3987 /* Unix has ${var-name} type environment vars */ | |
3988 if (*tail == '{' && !vim_isIDc('{')) | |
3989 { | |
3990 tail++; /* ignore '{' */ | |
3991 while (c-- > 0 && *tail && *tail != '}') | |
3992 *var++ = *tail++; | |
3993 } | |
3994 else | |
3995 #endif | |
3996 { | |
3997 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) | |
3998 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
3999 || (*src == '%' && *tail != '%') | |
4000 #endif | |
4001 )) | |
4002 { | |
4003 #ifdef OS2 /* env vars only in uppercase */ | |
4004 *var++ = TOUPPER_LOC(*tail); | |
4005 tail++; /* toupper() may be a macro! */ | |
4006 #else | |
4007 *var++ = *tail++; | |
4008 #endif | |
4009 } | |
4010 } | |
4011 | |
4012 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) | |
4013 # ifdef UNIX | |
4014 if (src[1] == '{' && *tail != '}') | |
4015 # else | |
4016 if (*src == '%' && *tail != '%') | |
4017 # endif | |
4018 var = NULL; | |
4019 else | |
4020 { | |
4021 # ifdef UNIX | |
4022 if (src[1] == '{') | |
4023 # else | |
4024 if (*src == '%') | |
4025 #endif | |
4026 ++tail; | |
4027 #endif | |
4028 *var = NUL; | |
4029 var = vim_getenv(dst, &mustfree); | |
4030 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) | |
4031 } | |
4032 #endif | |
4033 } | |
4034 /* home directory */ | |
4035 else if ( src[1] == NUL | |
4036 || vim_ispathsep(src[1]) | |
4037 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) | |
4038 { | |
4039 var = homedir; | |
4040 tail = src + 1; | |
4041 } | |
4042 else /* user directory */ | |
4043 { | |
4044 #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) | |
4045 /* | |
4046 * Copy ~user to dst[], so we can put a NUL after it. | |
4047 */ | |
4048 tail = src; | |
4049 var = dst; | |
4050 c = dstlen - 1; | |
4051 while ( c-- > 0 | |
4052 && *tail | |
4053 && vim_isfilec(*tail) | |
4054 && !vim_ispathsep(*tail)) | |
4055 *var++ = *tail++; | |
4056 *var = NUL; | |
4057 # ifdef UNIX | |
4058 /* | |
4059 * If the system supports getpwnam(), use it. | |
4060 * Otherwise, or if getpwnam() fails, the shell is used to | |
4061 * expand ~user. This is slower and may fail if the shell | |
4062 * does not support ~user (old versions of /bin/sh). | |
4063 */ | |
4064 # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) | |
4065 { | |
4066 struct passwd *pw; | |
4067 | |
626 | 4068 /* Note: memory allocated by getpwnam() is never freed. |
4069 * Calling endpwent() apparently doesn't help. */ | |
7 | 4070 pw = getpwnam((char *)dst + 1); |
4071 if (pw != NULL) | |
4072 var = (char_u *)pw->pw_dir; | |
4073 else | |
4074 var = NULL; | |
4075 } | |
4076 if (var == NULL) | |
4077 # endif | |
4078 { | |
4079 expand_T xpc; | |
4080 | |
4081 ExpandInit(&xpc); | |
4082 xpc.xp_context = EXPAND_FILES; | |
4083 var = ExpandOne(&xpc, dst, NULL, | |
4084 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); | |
4085 mustfree = TRUE; | |
4086 } | |
4087 | |
4088 # else /* !UNIX, thus VMS */ | |
4089 /* | |
4090 * USER_HOME is a comma-separated list of | |
4091 * directories to search for the user account in. | |
4092 */ | |
4093 { | |
4094 char_u test[MAXPATHL], paths[MAXPATHL]; | |
4095 char_u *path, *next_path, *ptr; | |
4096 struct stat st; | |
4097 | |
4098 STRCPY(paths, USER_HOME); | |
4099 next_path = paths; | |
4100 while (*next_path) | |
4101 { | |
4102 for (path = next_path; *next_path && *next_path != ','; | |
4103 next_path++); | |
4104 if (*next_path) | |
4105 *next_path++ = NUL; | |
4106 STRCPY(test, path); | |
4107 STRCAT(test, "/"); | |
4108 STRCAT(test, dst + 1); | |
4109 if (mch_stat(test, &st) == 0) | |
4110 { | |
4111 var = alloc(STRLEN(test) + 1); | |
4112 STRCPY(var, test); | |
4113 mustfree = TRUE; | |
4114 break; | |
4115 } | |
4116 } | |
4117 } | |
4118 # endif /* UNIX */ | |
4119 #else | |
4120 /* cannot expand user's home directory, so don't try */ | |
4121 var = NULL; | |
4122 tail = (char_u *)""; /* for gcc */ | |
4123 #endif /* UNIX || VMS */ | |
4124 } | |
4125 | |
4126 #ifdef BACKSLASH_IN_FILENAME | |
4127 /* If 'shellslash' is set change backslashes to forward slashes. | |
4128 * Can't use slash_adjust(), p_ssl may be set temporarily. */ | |
4129 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) | |
4130 { | |
4131 char_u *p = vim_strsave(var); | |
4132 | |
4133 if (p != NULL) | |
4134 { | |
4135 if (mustfree) | |
4136 vim_free(var); | |
4137 var = p; | |
4138 mustfree = TRUE; | |
4139 forward_slash(var); | |
4140 } | |
4141 } | |
4142 #endif | |
4143 | |
4144 /* If "var" contains white space, escape it with a backslash. | |
4145 * Required for ":e ~/tt" when $HOME includes a space. */ | |
4146 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) | |
4147 { | |
4148 char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); | |
4149 | |
4150 if (p != NULL) | |
4151 { | |
4152 if (mustfree) | |
4153 vim_free(var); | |
4154 var = p; | |
4155 mustfree = TRUE; | |
4156 } | |
4157 } | |
4158 | |
4159 if (var != NULL && *var != NUL | |
4160 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) | |
4161 { | |
4162 STRCPY(dst, var); | |
4163 dstlen -= (int)STRLEN(var); | |
835 | 4164 c = (int)STRLEN(var); |
7 | 4165 /* if var[] ends in a path separator and tail[] starts |
4166 * with it, skip a character */ | |
39 | 4167 if (*var != NUL && after_pathsep(dst, dst + c) |
7 | 4168 #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
4169 && dst[-1] != ':' | |
4170 #endif | |
4171 && vim_ispathsep(*tail)) | |
4172 ++tail; | |
39 | 4173 dst += c; |
7 | 4174 src = tail; |
4175 copy_char = FALSE; | |
4176 } | |
4177 if (mustfree) | |
4178 vim_free(var); | |
4179 } | |
4180 | |
4181 if (copy_char) /* copy at least one char */ | |
4182 { | |
4183 /* | |
1224 | 4184 * Recognize the start of a new name, for '~'. |
1408 | 4185 * Don't do this when "one" is TRUE, to avoid expanding "~" in |
4186 * ":edit foo ~ foo". | |
7 | 4187 */ |
4188 at_start = FALSE; | |
4189 if (src[0] == '\\' && src[1] != NUL) | |
4190 { | |
4191 *dst++ = *src++; | |
4192 --dstlen; | |
4193 } | |
1408 | 4194 else if ((src[0] == ' ' || src[0] == ',') && !one) |
7 | 4195 at_start = TRUE; |
4196 *dst++ = *src++; | |
4197 --dstlen; | |
374 | 4198 |
4199 if (startstr != NULL && src - startstr_len >= srcp | |
4200 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0) | |
4201 at_start = TRUE; | |
7 | 4202 } |
4203 } | |
4204 *dst = NUL; | |
4205 } | |
4206 | |
4207 /* | |
4208 * Vim's version of getenv(). | |
4209 * Special handling of $HOME, $VIM and $VIMRUNTIME. | |
196 | 4210 * Also does ACP to 'enc' conversion for Win32. |
2786 | 4211 * "mustfree" is set to TRUE when returned is allocated, it must be |
4212 * initialized to FALSE by the caller. | |
7 | 4213 */ |
4214 char_u * | |
4215 vim_getenv(name, mustfree) | |
4216 char_u *name; | |
2786 | 4217 int *mustfree; |
7 | 4218 { |
4219 char_u *p; | |
4220 char_u *pend; | |
4221 int vimruntime; | |
4222 | |
4223 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) | |
4224 /* use "C:/" when $HOME is not set */ | |
4225 if (STRCMP(name, "HOME") == 0) | |
4226 return homedir; | |
4227 #endif | |
4228 | |
4229 p = mch_getenv(name); | |
4230 if (p != NULL && *p == NUL) /* empty is the same as not set */ | |
4231 p = NULL; | |
4232 | |
4233 if (p != NULL) | |
170 | 4234 { |
4235 #if defined(FEAT_MBYTE) && defined(WIN3264) | |
4236 if (enc_utf8) | |
4237 { | |
4238 int len; | |
2786 | 4239 char_u *pp = NULL; |
170 | 4240 |
4241 /* Convert from active codepage to UTF-8. Other conversions are | |
4242 * not done, because they would fail for non-ASCII characters. */ | |
835 | 4243 acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
170 | 4244 if (pp != NULL) |
4245 { | |
4246 p = pp; | |
4247 *mustfree = TRUE; | |
4248 } | |
4249 } | |
4250 #endif | |
7 | 4251 return p; |
170 | 4252 } |
7 | 4253 |
4254 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); | |
4255 if (!vimruntime && STRCMP(name, "VIM") != 0) | |
4256 return NULL; | |
4257 | |
4258 /* | |
4259 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. | |
4260 * Don't do this when default_vimruntime_dir is non-empty. | |
4261 */ | |
4262 if (vimruntime | |
4263 #ifdef HAVE_PATHDEF | |
4264 && *default_vimruntime_dir == NUL | |
4265 #endif | |
4266 ) | |
4267 { | |
4268 p = mch_getenv((char_u *)"VIM"); | |
4269 if (p != NULL && *p == NUL) /* empty is the same as not set */ | |
4270 p = NULL; | |
4271 if (p != NULL) | |
4272 { | |
4273 p = vim_version_dir(p); | |
4274 if (p != NULL) | |
4275 *mustfree = TRUE; | |
4276 else | |
4277 p = mch_getenv((char_u *)"VIM"); | |
170 | 4278 |
4279 #if defined(FEAT_MBYTE) && defined(WIN3264) | |
4280 if (enc_utf8) | |
4281 { | |
4282 int len; | |
2786 | 4283 char_u *pp = NULL; |
170 | 4284 |
4285 /* Convert from active codepage to UTF-8. Other conversions | |
4286 * are not done, because they would fail for non-ASCII | |
4287 * characters. */ | |
835 | 4288 acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
170 | 4289 if (pp != NULL) |
4290 { | |
2786 | 4291 if (*mustfree) |
170 | 4292 vim_free(p); |
4293 p = pp; | |
4294 *mustfree = TRUE; | |
4295 } | |
4296 } | |
4297 #endif | |
7 | 4298 } |
4299 } | |
4300 | |
4301 /* | |
4302 * When expanding $VIM or $VIMRUNTIME fails, try using: | |
4303 * - the directory name from 'helpfile' (unless it contains '$') | |
4304 * - the executable name from argv[0] | |
4305 */ | |
4306 if (p == NULL) | |
4307 { | |
4308 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) | |
4309 p = p_hf; | |
4310 #ifdef USE_EXE_NAME | |
4311 /* | |
4312 * Use the name of the executable, obtained from argv[0]. | |
4313 */ | |
4314 else | |
4315 p = exe_name; | |
4316 #endif | |
4317 if (p != NULL) | |
4318 { | |
4319 /* remove the file name */ | |
4320 pend = gettail(p); | |
4321 | |
4322 /* remove "doc/" from 'helpfile', if present */ | |
4323 if (p == p_hf) | |
4324 pend = remove_tail(p, pend, (char_u *)"doc"); | |
4325 | |
4326 #ifdef USE_EXE_NAME | |
4327 # ifdef MACOS_X | |
768 | 4328 /* remove "MacOS" from exe_name and add "Resources/vim" */ |
7 | 4329 if (p == exe_name) |
4330 { | |
4331 char_u *pend1; | |
768 | 4332 char_u *pnew; |
4333 | |
4334 pend1 = remove_tail(p, pend, (char_u *)"MacOS"); | |
4335 if (pend1 != pend) | |
4336 { | |
4337 pnew = alloc((unsigned)(pend1 - p) + 15); | |
4338 if (pnew != NULL) | |
4339 { | |
4340 STRNCPY(pnew, p, (pend1 - p)); | |
4341 STRCPY(pnew + (pend1 - p), "Resources/vim"); | |
4342 p = pnew; | |
4343 pend = p + STRLEN(p); | |
4344 } | |
4345 } | |
7 | 4346 } |
4347 # endif | |
4348 /* remove "src/" from exe_name, if present */ | |
4349 if (p == exe_name) | |
4350 pend = remove_tail(p, pend, (char_u *)"src"); | |
4351 #endif | |
4352 | |
4353 /* for $VIM, remove "runtime/" or "vim54/", if present */ | |
4354 if (!vimruntime) | |
4355 { | |
4356 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); | |
4357 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); | |
4358 } | |
4359 | |
4360 /* remove trailing path separator */ | |
4361 #ifndef MACOS_CLASSIC | |
4362 /* With MacOS path (with colons) the final colon is required */ | |
1532 | 4363 /* to avoid confusion between absolute and relative path */ |
39 | 4364 if (pend > p && after_pathsep(p, pend)) |
7 | 4365 --pend; |
4366 #endif | |
4367 | |
768 | 4368 #ifdef MACOS_X |
4369 if (p == exe_name || p == p_hf) | |
4370 #endif | |
4371 /* check that the result is a directory name */ | |
4372 p = vim_strnsave(p, (int)(pend - p)); | |
7 | 4373 |
4374 if (p != NULL && !mch_isdir(p)) | |
4375 { | |
4376 vim_free(p); | |
4377 p = NULL; | |
4378 } | |
4379 else | |
4380 { | |
4381 #ifdef USE_EXE_NAME | |
4382 /* may add "/vim54" or "/runtime" if it exists */ | |
4383 if (vimruntime && (pend = vim_version_dir(p)) != NULL) | |
4384 { | |
4385 vim_free(p); | |
4386 p = pend; | |
4387 } | |
4388 #endif | |
4389 *mustfree = TRUE; | |
4390 } | |
4391 } | |
4392 } | |
4393 | |
4394 #ifdef HAVE_PATHDEF | |
4395 /* When there is a pathdef.c file we can use default_vim_dir and | |
4396 * default_vimruntime_dir */ | |
4397 if (p == NULL) | |
4398 { | |
4399 /* Only use default_vimruntime_dir when it is not empty */ | |
4400 if (vimruntime && *default_vimruntime_dir != NUL) | |
4401 { | |
4402 p = default_vimruntime_dir; | |
4403 *mustfree = FALSE; | |
4404 } | |
4405 else if (*default_vim_dir != NUL) | |
4406 { | |
4407 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) | |
4408 *mustfree = TRUE; | |
4409 else | |
4410 { | |
4411 p = default_vim_dir; | |
4412 *mustfree = FALSE; | |
4413 } | |
4414 } | |
4415 } | |
4416 #endif | |
4417 | |
4418 /* | |
4419 * Set the environment variable, so that the new value can be found fast | |
4420 * next time, and others can also use it (e.g. Perl). | |
4421 */ | |
4422 if (p != NULL) | |
4423 { | |
4424 if (vimruntime) | |
4425 { | |
4426 vim_setenv((char_u *)"VIMRUNTIME", p); | |
4427 didset_vimruntime = TRUE; | |
4428 } | |
4429 else | |
4430 { | |
4431 vim_setenv((char_u *)"VIM", p); | |
4432 didset_vim = TRUE; | |
4433 } | |
4434 } | |
4435 return p; | |
4436 } | |
4437 | |
4438 /* | |
4439 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. | |
4440 * Return NULL if not, return its name in allocated memory otherwise. | |
4441 */ | |
4442 static char_u * | |
4443 vim_version_dir(vimdir) | |
4444 char_u *vimdir; | |
4445 { | |
4446 char_u *p; | |
4447 | |
4448 if (vimdir == NULL || *vimdir == NUL) | |
4449 return NULL; | |
4450 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); | |
4451 if (p != NULL && mch_isdir(p)) | |
4452 return p; | |
4453 vim_free(p); | |
4454 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); | |
4455 if (p != NULL && mch_isdir(p)) | |
4456 return p; | |
4457 vim_free(p); | |
4458 return NULL; | |
4459 } | |
4460 | |
4461 /* | |
4462 * If the string between "p" and "pend" ends in "name/", return "pend" minus | |
4463 * the length of "name/". Otherwise return "pend". | |
4464 */ | |
4465 static char_u * | |
4466 remove_tail(p, pend, name) | |
4467 char_u *p; | |
4468 char_u *pend; | |
4469 char_u *name; | |
4470 { | |
4471 int len = (int)STRLEN(name) + 1; | |
4472 char_u *newend = pend - len; | |
4473 | |
4474 if (newend >= p | |
4475 && fnamencmp(newend, name, len - 1) == 0 | |
39 | 4476 && (newend == p || after_pathsep(p, newend))) |
7 | 4477 return newend; |
4478 return pend; | |
4479 } | |
4480 | |
4481 /* | |
4482 * Our portable version of setenv. | |
4483 */ | |
4484 void | |
4485 vim_setenv(name, val) | |
4486 char_u *name; | |
4487 char_u *val; | |
4488 { | |
4489 #ifdef HAVE_SETENV | |
4490 mch_setenv((char *)name, (char *)val, 1); | |
4491 #else | |
4492 char_u *envbuf; | |
4493 | |
4494 /* | |
4495 * Putenv does not copy the string, it has to remain | |
4496 * valid. The allocated memory will never be freed. | |
4497 */ | |
4498 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); | |
4499 if (envbuf != NULL) | |
4500 { | |
4501 sprintf((char *)envbuf, "%s=%s", name, val); | |
4502 putenv((char *)envbuf); | |
4503 } | |
4504 #endif | |
3382 | 4505 #ifdef FEAT_GETTEXT |
4506 /* | |
4507 * When setting $VIMRUNTIME adjust the directory to find message | |
4508 * translations to $VIMRUNTIME/lang. | |
4509 */ | |
4510 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) | |
4511 { | |
4512 char_u *buf = concat_str(val, (char_u *)"/lang"); | |
4513 | |
4514 if (buf != NULL) | |
4515 { | |
4516 bindtextdomain(VIMPACKAGE, (char *)buf); | |
4517 vim_free(buf); | |
4518 } | |
4519 } | |
4520 #endif | |
7 | 4521 } |
4522 | |
4523 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
4524 /* | |
4525 * Function given to ExpandGeneric() to obtain an environment variable name. | |
4526 */ | |
4527 char_u * | |
4528 get_env_name(xp, idx) | |
1883 | 4529 expand_T *xp UNUSED; |
7 | 4530 int idx; |
4531 { | |
4532 # if defined(AMIGA) || defined(__MRC__) || defined(__SC__) | |
4533 /* | |
4534 * No environ[] on the Amiga and on the Mac (using MPW). | |
4535 */ | |
4536 return NULL; | |
4537 # else | |
4538 # ifndef __WIN32__ | |
4539 /* Borland C++ 5.2 has this in a header file. */ | |
4540 extern char **environ; | |
4541 # endif | |
17 | 4542 # define ENVNAMELEN 100 |
4543 static char_u name[ENVNAMELEN]; | |
7 | 4544 char_u *str; |
4545 int n; | |
4546 | |
4547 str = (char_u *)environ[idx]; | |
4548 if (str == NULL) | |
4549 return NULL; | |
4550 | |
17 | 4551 for (n = 0; n < ENVNAMELEN - 1; ++n) |
7 | 4552 { |
4553 if (str[n] == '=' || str[n] == NUL) | |
4554 break; | |
4555 name[n] = str[n]; | |
4556 } | |
4557 name[n] = NUL; | |
4558 return name; | |
4559 # endif | |
4560 } | |
3744 | 4561 |
4562 /* | |
4563 * Find all user names for user completion. | |
4564 * Done only once and then cached. | |
4565 */ | |
4566 static void | |
4938
bcb84438bb5b
updated for version 7.3.1214
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4567 init_users() |
bcb84438bb5b
updated for version 7.3.1214
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4568 { |
3744 | 4569 static int lazy_init_done = FALSE; |
4570 | |
4571 if (lazy_init_done) | |
4572 return; | |
4573 | |
4574 lazy_init_done = TRUE; | |
4575 ga_init2(&ga_users, sizeof(char_u *), 20); | |
4576 | |
4577 # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) | |
4578 { | |
4579 char_u* user; | |
4580 struct passwd* pw; | |
4581 | |
4582 setpwent(); | |
4583 while ((pw = getpwent()) != NULL) | |
4584 /* pw->pw_name shouldn't be NULL but just in case... */ | |
4585 if (pw->pw_name != NULL) | |
4586 { | |
4587 if (ga_grow(&ga_users, 1) == FAIL) | |
4588 break; | |
4589 user = vim_strsave((char_u*)pw->pw_name); | |
4590 if (user == NULL) | |
4591 break; | |
4592 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user; | |
4593 } | |
4594 endpwent(); | |
4595 } | |
4596 # endif | |
4597 } | |
4598 | |
4599 /* | |
4600 * Function given to ExpandGeneric() to obtain an user names. | |
4601 */ | |
4602 char_u* | |
4603 get_users(xp, idx) | |
4604 expand_T *xp UNUSED; | |
4605 int idx; | |
4606 { | |
4607 init_users(); | |
4608 if (idx < ga_users.ga_len) | |
4609 return ((char_u **)ga_users.ga_data)[idx]; | |
4610 return NULL; | |
4611 } | |
4612 | |
4613 /* | |
4614 * Check whether name matches a user name. Return: | |
4615 * 0 if name does not match any user name. | |
4616 * 1 if name partially matches the beginning of a user name. | |
4617 * 2 is name fully matches a user name. | |
4618 */ | |
4619 int match_user(name) | |
4620 char_u* name; | |
4621 { | |
4622 int i; | |
4623 int n = (int)STRLEN(name); | |
4624 int result = 0; | |
4625 | |
4626 init_users(); | |
4627 for (i = 0; i < ga_users.ga_len; i++) | |
4628 { | |
4629 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) | |
4630 return 2; /* full match */ | |
4631 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) | |
4632 result = 1; /* partial match */ | |
4633 } | |
4634 return result; | |
4635 } | |
7 | 4636 #endif |
4637 | |
4638 /* | |
4639 * Replace home directory by "~" in each space or comma separated file name in | |
4640 * 'src'. | |
4641 * If anything fails (except when out of space) dst equals src. | |
4642 */ | |
4643 void | |
4644 home_replace(buf, src, dst, dstlen, one) | |
4645 buf_T *buf; /* when not NULL, check for help files */ | |
4646 char_u *src; /* input file name */ | |
4647 char_u *dst; /* where to put the result */ | |
4648 int dstlen; /* maximum length of the result */ | |
4649 int one; /* if TRUE, only replace one file name, include | |
4650 spaces and commas in the file name. */ | |
4651 { | |
4652 size_t dirlen = 0, envlen = 0; | |
4653 size_t len; | |
3598 | 4654 char_u *homedir_env, *homedir_env_orig; |
7 | 4655 char_u *p; |
4656 | |
4657 if (src == NULL) | |
4658 { | |
4659 *dst = NUL; | |
4660 return; | |
4661 } | |
4662 | |
4663 /* | |
4664 * If the file is a help file, remove the path completely. | |
4665 */ | |
4666 if (buf != NULL && buf->b_help) | |
4667 { | |
4668 STRCPY(dst, gettail(src)); | |
4669 return; | |
4670 } | |
4671 | |
4672 /* | |
4673 * We check both the value of the $HOME environment variable and the | |
4674 * "real" home directory. | |
4675 */ | |
4676 if (homedir != NULL) | |
4677 dirlen = STRLEN(homedir); | |
4678 | |
4679 #ifdef VMS | |
3598 | 4680 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
7 | 4681 #else |
3598 | 4682 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
4683 #endif | |
3658 | 4684 /* Empty is the same as not set. */ |
4685 if (homedir_env != NULL && *homedir_env == NUL) | |
4686 homedir_env = NULL; | |
4687 | |
4752
77ecab3bb207
updated for version 7.3.1123
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
4688 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) |
3658 | 4689 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL) |
3598 | 4690 { |
4691 int usedlen = 0; | |
4692 int flen; | |
4693 char_u *fbuf = NULL; | |
4694 | |
4695 flen = (int)STRLEN(homedir_env); | |
3608 | 4696 (void)modify_fname((char_u *)":p", &usedlen, |
4697 &homedir_env, &fbuf, &flen); | |
3598 | 4698 flen = (int)STRLEN(homedir_env); |
4699 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) | |
4700 /* Remove the trailing / that is added to a directory. */ | |
4701 homedir_env[flen - 1] = NUL; | |
4702 } | |
7 | 4703 #endif |
4704 | |
4705 if (homedir_env != NULL) | |
4706 envlen = STRLEN(homedir_env); | |
4707 | |
4708 if (!one) | |
4709 src = skipwhite(src); | |
4710 while (*src && dstlen > 0) | |
4711 { | |
4712 /* | |
4713 * Here we are at the beginning of a file name. | |
4714 * First, check to see if the beginning of the file name matches | |
4715 * $HOME or the "real" home directory. Check that there is a '/' | |
4716 * after the match (so that if e.g. the file is "/home/pieter/bla", | |
4717 * and the home directory is "/home/piet", the file does not end up | |
4718 * as "~er/bla" (which would seem to indicate the file "bla" in user | |
4719 * er's home directory)). | |
4720 */ | |
4721 p = homedir; | |
4722 len = dirlen; | |
4723 for (;;) | |
4724 { | |
4725 if ( len | |
4726 && fnamencmp(src, p, len) == 0 | |
4727 && (vim_ispathsep(src[len]) | |
4728 || (!one && (src[len] == ',' || src[len] == ' ')) | |
4729 || src[len] == NUL)) | |
4730 { | |
4731 src += len; | |
4732 if (--dstlen > 0) | |
4733 *dst++ = '~'; | |
4734 | |
4735 /* | |
4736 * If it's just the home directory, add "/". | |
4737 */ | |
4738 if (!vim_ispathsep(src[0]) && --dstlen > 0) | |
4739 *dst++ = '/'; | |
4740 break; | |
4741 } | |
4742 if (p == homedir_env) | |
4743 break; | |
4744 p = homedir_env; | |
4745 len = envlen; | |
4746 } | |
4747 | |
4748 /* if (!one) skip to separator: space or comma */ | |
4749 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) | |
4750 *dst++ = *src++; | |
4751 /* skip separator */ | |
4752 while ((*src == ' ' || *src == ',') && --dstlen > 0) | |
4753 *dst++ = *src++; | |
4754 } | |
4755 /* if (dstlen == 0) out of space, what to do??? */ | |
4756 | |
4757 *dst = NUL; | |
3598 | 4758 |
4759 if (homedir_env != homedir_env_orig) | |
4760 vim_free(homedir_env); | |
7 | 4761 } |
4762 | |
4763 /* | |
4764 * Like home_replace, store the replaced string in allocated memory. | |
4765 * When something fails, NULL is returned. | |
4766 */ | |
4767 char_u * | |
4768 home_replace_save(buf, src) | |
4769 buf_T *buf; /* when not NULL, check for help files */ | |
4770 char_u *src; /* input file name */ | |
4771 { | |
4772 char_u *dst; | |
4773 unsigned len; | |
4774 | |
4775 len = 3; /* space for "~/" and trailing NUL */ | |
4776 if (src != NULL) /* just in case */ | |
4777 len += (unsigned)STRLEN(src); | |
4778 dst = alloc(len); | |
4779 if (dst != NULL) | |
4780 home_replace(buf, src, dst, len, TRUE); | |
4781 return dst; | |
4782 } | |
4783 | |
4784 /* | |
4785 * Compare two file names and return: | |
4786 * FPC_SAME if they both exist and are the same file. | |
4787 * FPC_SAMEX if they both don't exist and have the same file name. | |
4788 * FPC_DIFF if they both exist and are different files. | |
4789 * FPC_NOTX if they both don't exist. | |
4790 * FPC_DIFFX if one of them doesn't exist. | |
4791 * For the first name environment variables are expanded | |
4792 */ | |
4793 int | |
4794 fullpathcmp(s1, s2, checkname) | |
4795 char_u *s1, *s2; | |
4796 int checkname; /* when both don't exist, check file names */ | |
4797 { | |
4798 #ifdef UNIX | |
4799 char_u exp1[MAXPATHL]; | |
4800 char_u full1[MAXPATHL]; | |
4801 char_u full2[MAXPATHL]; | |
4802 struct stat st1, st2; | |
4803 int r1, r2; | |
4804 | |
4805 expand_env(s1, exp1, MAXPATHL); | |
4806 r1 = mch_stat((char *)exp1, &st1); | |
4807 r2 = mch_stat((char *)s2, &st2); | |
4808 if (r1 != 0 && r2 != 0) | |
4809 { | |
4810 /* if mch_stat() doesn't work, may compare the names */ | |
4811 if (checkname) | |
4812 { | |
4813 if (fnamecmp(exp1, s2) == 0) | |
4814 return FPC_SAMEX; | |
4815 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
4816 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
4817 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) | |
4818 return FPC_SAMEX; | |
4819 } | |
4820 return FPC_NOTX; | |
4821 } | |
4822 if (r1 != 0 || r2 != 0) | |
4823 return FPC_DIFFX; | |
4824 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) | |
4825 return FPC_SAME; | |
4826 return FPC_DIFF; | |
4827 #else | |
4828 char_u *exp1; /* expanded s1 */ | |
4829 char_u *full1; /* full path of s1 */ | |
4830 char_u *full2; /* full path of s2 */ | |
4831 int retval = FPC_DIFF; | |
4832 int r1, r2; | |
4833 | |
4834 /* allocate one buffer to store three paths (alloc()/free() is slow!) */ | |
4835 if ((exp1 = alloc(MAXPATHL * 3)) != NULL) | |
4836 { | |
4837 full1 = exp1 + MAXPATHL; | |
4838 full2 = full1 + MAXPATHL; | |
4839 | |
4840 expand_env(s1, exp1, MAXPATHL); | |
4841 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
4842 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
4843 | |
4844 /* If vim_FullName() fails, the file probably doesn't exist. */ | |
4845 if (r1 != OK && r2 != OK) | |
4846 { | |
4847 if (checkname && fnamecmp(exp1, s2) == 0) | |
4848 retval = FPC_SAMEX; | |
4849 else | |
4850 retval = FPC_NOTX; | |
4851 } | |
4852 else if (r1 != OK || r2 != OK) | |
4853 retval = FPC_DIFFX; | |
4854 else if (fnamecmp(full1, full2)) | |
4855 retval = FPC_DIFF; | |
4856 else | |
4857 retval = FPC_SAME; | |
4858 vim_free(exp1); | |
4859 } | |
4860 return retval; | |
4861 #endif | |
4862 } | |
4863 | |
4864 /* | |
10 | 4865 * 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
|
4866 * When the path ends in a path separator the tail is the NUL after it. |
10 | 4867 * Fail safe: never returns NULL. |
7 | 4868 */ |
4869 char_u * | |
4870 gettail(fname) | |
4871 char_u *fname; | |
4872 { | |
4873 char_u *p1, *p2; | |
4874 | |
4875 if (fname == NULL) | |
4876 return (char_u *)""; | |
5432 | 4877 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */ |
4878 { | |
4879 if (vim_ispathsep_nocolon(*p2)) | |
7 | 4880 p1 = p2 + 1; |
39 | 4881 mb_ptr_adv(p2); |
7 | 4882 } |
4883 return p1; | |
4884 } | |
4885 | |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4886 #if defined(FEAT_SEARCHPATH) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4887 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
|
4888 |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4889 /* |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4890 * 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
|
4891 * separator: |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4892 * "/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
|
4893 * ^ ^ ^ ^ |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4894 */ |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4895 static char_u * |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4896 gettail_dir(fname) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4897 char_u *fname; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4898 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4899 char_u *dir_end = fname; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4900 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
|
4901 int look_for_sep = TRUE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4902 char_u *p; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4903 |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4904 for (p = fname; *p != NUL; ) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4905 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4906 if (vim_ispathsep(*p)) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4907 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4908 if (look_for_sep) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4909 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4910 next_dir_end = p; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4911 look_for_sep = FALSE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4912 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4913 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4914 else |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4915 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4916 if (!look_for_sep) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4917 dir_end = next_dir_end; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4918 look_for_sep = TRUE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4919 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4920 mb_ptr_adv(p); |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4921 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4922 return dir_end; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4923 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4924 #endif |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4925 |
7 | 4926 /* |
39 | 4927 * Get pointer to tail of "fname", including path separators. Putting a NUL |
4928 * here leaves the directory name. Takes care of "c:/" and "//". | |
4929 * Always returns a valid pointer. | |
4930 */ | |
4931 char_u * | |
4932 gettail_sep(fname) | |
4933 char_u *fname; | |
4934 { | |
4935 char_u *p; | |
4936 char_u *t; | |
4937 | |
4938 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ | |
4939 t = gettail(fname); | |
4940 while (t > p && after_pathsep(fname, t)) | |
4941 --t; | |
4942 #ifdef VMS | |
4943 /* path separator is part of the path */ | |
4944 ++t; | |
4945 #endif | |
4946 return t; | |
4947 } | |
4948 | |
4949 /* | |
7 | 4950 * get the next path component (just after the next path separator). |
4951 */ | |
4952 char_u * | |
4953 getnextcomp(fname) | |
4954 char_u *fname; | |
4955 { | |
4956 while (*fname && !vim_ispathsep(*fname)) | |
39 | 4957 mb_ptr_adv(fname); |
7 | 4958 if (*fname) |
4959 ++fname; | |
4960 return fname; | |
4961 } | |
4962 | |
4963 /* | |
4964 * Get a pointer to one character past the head of a path name. | |
4965 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. | |
4966 * If there is no head, path is returned. | |
4967 */ | |
4968 char_u * | |
4969 get_past_head(path) | |
4970 char_u *path; | |
4971 { | |
4972 char_u *retval; | |
4973 | |
4974 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
4975 /* may skip "c:" */ | |
4976 if (isalpha(path[0]) && path[1] == ':') | |
4977 retval = path + 2; | |
4978 else | |
4979 retval = path; | |
4980 #else | |
4981 # if defined(AMIGA) | |
4982 /* may skip "label:" */ | |
4983 retval = vim_strchr(path, ':'); | |
4984 if (retval == NULL) | |
4985 retval = path; | |
4986 # else /* Unix */ | |
4987 retval = path; | |
4988 # endif | |
4989 #endif | |
4990 | |
4991 while (vim_ispathsep(*retval)) | |
4992 ++retval; | |
4993 | |
4994 return retval; | |
4995 } | |
4996 | |
4997 /* | |
5432 | 4998 * Return TRUE if 'c' is a path separator. |
4999 * Note that for MS-Windows this includes the colon. | |
7 | 5000 */ |
5001 int | |
5002 vim_ispathsep(c) | |
5003 int c; | |
5004 { | |
2823 | 5005 #ifdef UNIX |
7 | 5006 return (c == '/'); /* UNIX has ':' inside file names */ |
2823 | 5007 #else |
5008 # ifdef BACKSLASH_IN_FILENAME | |
7 | 5009 return (c == ':' || c == '/' || c == '\\'); |
2823 | 5010 # else |
5011 # ifdef VMS | |
7 | 5012 /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
5013 return (c == ':' || c == '[' || c == ']' || c == '/' | |
5014 || c == '<' || c == '>' || c == '"' ); | |
2823 | 5015 # else |
7 | 5016 return (c == ':' || c == '/'); |
2823 | 5017 # endif /* VMS */ |
7 | 5018 # endif |
2823 | 5019 #endif |
7 | 5020 } |
5021 | |
5432 | 5022 /* |
5023 * Like vim_ispathsep(c), but exclude the colon for MS-Windows. | |
5024 */ | |
5025 int | |
5026 vim_ispathsep_nocolon(c) | |
5027 int c; | |
5028 { | |
5029 return vim_ispathsep(c) | |
5030 #ifdef BACKSLASH_IN_FILENAME | |
5031 && c != ':' | |
5032 #endif | |
5033 ; | |
5034 } | |
5035 | |
7 | 5036 #if defined(FEAT_SEARCHPATH) || defined(PROTO) |
5037 /* | |
5038 * return TRUE if 'c' is a path list separator. | |
5039 */ | |
5040 int | |
5041 vim_ispathlistsep(c) | |
5042 int c; | |
5043 { | |
5044 #ifdef UNIX | |
5045 return (c == ':'); | |
5046 #else | |
1224 | 5047 return (c == ';'); /* might not be right for every system... */ |
7 | 5048 #endif |
5049 } | |
5050 #endif | |
5051 | |
819 | 5052 #if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \ |
5053 || defined(FEAT_EVAL) || defined(PROTO) | |
5054 /* | |
5055 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" | |
5056 * It's done in-place. | |
5057 */ | |
5058 void | |
5059 shorten_dir(str) | |
5060 char_u *str; | |
5061 { | |
5062 char_u *tail, *s, *d; | |
5063 int skip = FALSE; | |
5064 | |
5065 tail = gettail(str); | |
5066 d = str; | |
5067 for (s = str; ; ++s) | |
5068 { | |
5069 if (s >= tail) /* copy the whole tail */ | |
5070 { | |
5071 *d++ = *s; | |
5072 if (*s == NUL) | |
5073 break; | |
5074 } | |
5075 else if (vim_ispathsep(*s)) /* copy '/' and next char */ | |
5076 { | |
5077 *d++ = *s; | |
5078 skip = FALSE; | |
5079 } | |
5080 else if (!skip) | |
5081 { | |
5082 *d++ = *s; /* copy next char */ | |
5083 if (*s != '~' && *s != '.') /* and leading "~" and "." */ | |
5084 skip = TRUE; | |
5085 # ifdef FEAT_MBYTE | |
5086 if (has_mbyte) | |
5087 { | |
5088 int l = mb_ptr2len(s); | |
5089 | |
5090 while (--l > 0) | |
927 | 5091 *d++ = *++s; |
819 | 5092 } |
5093 # endif | |
5094 } | |
5095 } | |
5096 } | |
5097 #endif | |
5098 | |
594 | 5099 /* |
5100 * Return TRUE if the directory of "fname" exists, FALSE otherwise. | |
5101 * Also returns TRUE if there is no directory name. | |
5102 * "fname" must be writable!. | |
5103 */ | |
5104 int | |
5105 dir_of_file_exists(fname) | |
5106 char_u *fname; | |
5107 { | |
5108 char_u *p; | |
5109 int c; | |
5110 int retval; | |
5111 | |
5112 p = gettail_sep(fname); | |
5113 if (p == fname) | |
5114 return TRUE; | |
5115 c = *p; | |
5116 *p = NUL; | |
5117 retval = mch_isdir(fname); | |
5118 *p = c; | |
5119 return retval; | |
5120 } | |
5121 | |
4242 | 5122 /* |
5123 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally | |
5124 * and deal with 'fileignorecase'. | |
7 | 5125 */ |
5126 int | |
5127 vim_fnamecmp(x, y) | |
5128 char_u *x, *y; | |
5129 { | |
4242 | 5130 #ifdef BACKSLASH_IN_FILENAME |
7 | 5131 return vim_fnamencmp(x, y, MAXPATHL); |
4242 | 5132 #else |
5133 if (p_fic) | |
5134 return MB_STRICMP(x, y); | |
5135 return STRCMP(x, y); | |
5136 #endif | |
7 | 5137 } |
5138 | |
5139 int | |
5140 vim_fnamencmp(x, y, len) | |
5141 char_u *x, *y; | |
5142 size_t len; | |
5143 { | |
4242 | 5144 #ifdef BACKSLASH_IN_FILENAME |
4246 | 5145 char_u *px = x; |
5146 char_u *py = y; | |
5147 int cx = NUL; | |
5148 int cy = NUL; | |
5149 | |
5150 while (len > 0) | |
5151 { | |
5152 cx = PTR2CHAR(px); | |
5153 cy = PTR2CHAR(py); | |
5154 if (cx == NUL || cy == NUL | |
5155 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) | |
5156 && !(cx == '/' && cy == '\\') | |
5157 && !(cx == '\\' && cy == '/'))) | |
7 | 5158 break; |
4246 | 5159 len -= MB_PTR2LEN(px); |
5160 px += MB_PTR2LEN(px); | |
5161 py += MB_PTR2LEN(py); | |
7 | 5162 } |
5163 if (len == 0) | |
5164 return 0; | |
4246 | 5165 return (cx - cy); |
4242 | 5166 #else |
5167 if (p_fic) | |
5168 return MB_STRNICMP(x, y, len); | |
5169 return STRNCMP(x, y, len); | |
5170 #endif | |
5171 } | |
7 | 5172 |
5173 /* | |
5174 * Concatenate file names fname1 and fname2 into allocated memory. | |
1224 | 5175 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
7 | 5176 */ |
5177 char_u * | |
5178 concat_fnames(fname1, fname2, sep) | |
5179 char_u *fname1; | |
5180 char_u *fname2; | |
5181 int sep; | |
5182 { | |
5183 char_u *dest; | |
5184 | |
5185 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); | |
5186 if (dest != NULL) | |
5187 { | |
5188 STRCPY(dest, fname1); | |
5189 if (sep) | |
5190 add_pathsep(dest); | |
5191 STRCAT(dest, fname2); | |
5192 } | |
5193 return dest; | |
5194 } | |
5195 | |
115 | 5196 /* |
5197 * Concatenate two strings and return the result in allocated memory. | |
5198 * Returns NULL when out of memory. | |
5199 */ | |
5200 char_u * | |
5201 concat_str(str1, str2) | |
5202 char_u *str1; | |
5203 char_u *str2; | |
5204 { | |
5205 char_u *dest; | |
5206 size_t l = STRLEN(str1); | |
5207 | |
5208 dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); | |
5209 if (dest != NULL) | |
5210 { | |
5211 STRCPY(dest, str1); | |
5212 STRCPY(dest + l, str2); | |
5213 } | |
5214 return dest; | |
5215 } | |
5216 | |
7 | 5217 /* |
5218 * Add a path separator to a file name, unless it already ends in a path | |
5219 * separator. | |
5220 */ | |
5221 void | |
5222 add_pathsep(p) | |
5223 char_u *p; | |
5224 { | |
39 | 5225 if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
7 | 5226 STRCAT(p, PATHSEPSTR); |
5227 } | |
5228 | |
5229 /* | |
5230 * FullName_save - Make an allocated copy of a full file name. | |
5231 * Returns NULL when out of memory. | |
5232 */ | |
5233 char_u * | |
5234 FullName_save(fname, force) | |
5235 char_u *fname; | |
3584 | 5236 int force; /* force expansion, even when it already looks |
5237 * like a full path name */ | |
7 | 5238 { |
5239 char_u *buf; | |
5240 char_u *new_fname = NULL; | |
5241 | |
5242 if (fname == NULL) | |
5243 return NULL; | |
5244 | |
5245 buf = alloc((unsigned)MAXPATHL); | |
5246 if (buf != NULL) | |
5247 { | |
5248 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) | |
5249 new_fname = vim_strsave(buf); | |
5250 else | |
5251 new_fname = vim_strsave(fname); | |
5252 vim_free(buf); | |
5253 } | |
5254 return new_fname; | |
5255 } | |
5256 | |
5257 #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) | |
5258 | |
5259 static char_u *skip_string __ARGS((char_u *p)); | |
5442 | 5260 static pos_T *ind_find_start_comment __ARGS((void)); |
7 | 5261 |
5262 /* | |
5263 * Find the start of a comment, not knowing if we are in a comment right now. | |
5264 * Search starts at w_cursor.lnum and goes backwards. | |
5265 */ | |
5442 | 5266 static pos_T * |
5267 ind_find_start_comment() /* XXX */ | |
5268 { | |
5269 return find_start_comment(curbuf->b_ind_maxcomment); | |
5270 } | |
5271 | |
7 | 5272 pos_T * |
5273 find_start_comment(ind_maxcomment) /* XXX */ | |
5274 int ind_maxcomment; | |
5275 { | |
5276 pos_T *pos; | |
5277 char_u *line; | |
5278 char_u *p; | |
829 | 5279 int cur_maxcomment = ind_maxcomment; |
5280 | |
5281 for (;;) | |
5282 { | |
5283 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); | |
5284 if (pos == NULL) | |
5285 break; | |
5286 | |
5287 /* | |
5288 * Check if the comment start we found is inside a string. | |
5289 * If it is then restrict the search to below this line and try again. | |
5290 */ | |
5291 line = ml_get(pos->lnum); | |
1883 | 5292 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
829 | 5293 p = skip_string(p); |
1883 | 5294 if ((colnr_T)(p - line) <= pos->col) |
829 | 5295 break; |
5296 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; | |
5297 if (cur_maxcomment <= 0) | |
5298 { | |
5299 pos = NULL; | |
5300 break; | |
5301 } | |
5302 } | |
7 | 5303 return pos; |
5304 } | |
5305 | |
5306 /* | |
5307 * Skip to the end of a "string" and a 'c' character. | |
5308 * If there is no string or character, return argument unmodified. | |
5309 */ | |
5310 static char_u * | |
5311 skip_string(p) | |
5312 char_u *p; | |
5313 { | |
5314 int i; | |
5315 | |
5316 /* | |
5317 * We loop, because strings may be concatenated: "date""time". | |
5318 */ | |
5319 for ( ; ; ++p) | |
5320 { | |
5321 if (p[0] == '\'') /* 'c' or '\n' or '\000' */ | |
5322 { | |
5323 if (!p[1]) /* ' at end of line */ | |
5324 break; | |
5325 i = 2; | |
5326 if (p[1] == '\\') /* '\n' or '\000' */ | |
5327 { | |
5328 ++i; | |
5329 while (vim_isdigit(p[i - 1])) /* '\000' */ | |
5330 ++i; | |
5331 } | |
5332 if (p[i] == '\'') /* check for trailing ' */ | |
5333 { | |
5334 p += i; | |
5335 continue; | |
5336 } | |
5337 } | |
5338 else if (p[0] == '"') /* start of string */ | |
5339 { | |
5340 for (++p; p[0]; ++p) | |
5341 { | |
5342 if (p[0] == '\\' && p[1] != NUL) | |
5343 ++p; | |
5344 else if (p[0] == '"') /* end of string */ | |
5345 break; | |
5346 } | |
5347 if (p[0] == '"') | |
5348 continue; | |
5349 } | |
5350 break; /* no string found */ | |
5351 } | |
5352 if (!*p) | |
5353 --p; /* backup from NUL */ | |
5354 return p; | |
5355 } | |
5356 #endif /* FEAT_CINDENT || FEAT_SYN_HL */ | |
5357 | |
5358 #if defined(FEAT_CINDENT) || defined(PROTO) | |
5359 | |
5360 /* | |
5361 * Do C or expression indenting on the current line. | |
5362 */ | |
5363 void | |
5364 do_c_expr_indent() | |
5365 { | |
5366 # ifdef FEAT_EVAL | |
5367 if (*curbuf->b_p_inde != NUL) | |
5368 fixthisline(get_expr_indent); | |
5369 else | |
5370 # endif | |
5371 fixthisline(get_c_indent); | |
5372 } | |
5373 | |
5374 /* | |
5375 * Functions for C-indenting. | |
5376 * Most of this originally comes from Eric Fischer. | |
5377 */ | |
5378 /* | |
5379 * Below "XXX" means that this function may unlock the current line. | |
5380 */ | |
5381 | |
5382 static char_u *cin_skipcomment __ARGS((char_u *)); | |
5383 static int cin_nocode __ARGS((char_u *)); | |
5384 static pos_T *find_line_comment __ARGS((void)); | |
6030 | 5385 static int cin_has_js_key __ARGS((char_u *text)); |
7 | 5386 static int cin_islabel_skip __ARGS((char_u **)); |
5387 static int cin_isdefault __ARGS((char_u *)); | |
5388 static char_u *after_label __ARGS((char_u *l)); | |
5389 static int get_indent_nolabel __ARGS((linenr_T lnum)); | |
5442 | 5390 static int skip_label __ARGS((linenr_T, char_u **pp)); |
7 | 5391 static int cin_first_id_amount __ARGS((void)); |
5392 static int cin_get_equal_amount __ARGS((linenr_T lnum)); | |
5393 static int cin_ispreproc __ARGS((char_u *)); | |
5394 static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump)); | |
5395 static int cin_iscomment __ARGS((char_u *)); | |
5396 static int cin_islinecomment __ARGS((char_u *)); | |
5397 static int cin_isterminated __ARGS((char_u *, int, int)); | |
5398 static int cin_isinit __ARGS((void)); | |
5442 | 5399 static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T)); |
7 | 5400 static int cin_isif __ARGS((char_u *)); |
5401 static int cin_iselse __ARGS((char_u *)); | |
5402 static int cin_isdo __ARGS((char_u *)); | |
5442 | 5403 static int cin_iswhileofdo __ARGS((char_u *, linenr_T)); |
3461 | 5404 static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset)); |
5442 | 5405 static int cin_iswhileofdo_end __ARGS((int terminated)); |
7 | 5406 static int cin_isbreak __ARGS((char_u *)); |
1336 | 5407 static int cin_is_cpp_baseclass __ARGS((colnr_T *col)); |
5442 | 5408 static int get_baseclass_amount __ARGS((int col)); |
7 | 5409 static int cin_ends_in __ARGS((char_u *, char_u *, char_u *)); |
4184 | 5410 static int cin_starts_with __ARGS((char_u *s, char *word)); |
7 | 5411 static int cin_skip2pos __ARGS((pos_T *trypos)); |
5442 | 5412 static pos_T *find_start_brace __ARGS((void)); |
5413 static pos_T *find_match_paren __ARGS((int)); | |
6030 | 5414 static pos_T *find_match_char __ARGS((int c, int ind_maxparen)); |
5442 | 5415 static int corr_ind_maxparen __ARGS((pos_T *startpos)); |
7 | 5416 static int find_last_paren __ARGS((char_u *l, int start, int end)); |
5442 | 5417 static int find_match __ARGS((int lookfor, linenr_T ourscope)); |
2857 | 5418 static int cin_is_cpp_namespace __ARGS((char_u *)); |
7 | 5419 |
5420 /* | |
5421 * Skip over white space and C comments within the line. | |
1096 | 5422 * Also skip over Perl/shell comments if desired. |
7 | 5423 */ |
5424 static char_u * | |
5425 cin_skipcomment(s) | |
5426 char_u *s; | |
5427 { | |
5428 while (*s) | |
5429 { | |
1096 | 5430 char_u *prev_s = s; |
5431 | |
7 | 5432 s = skipwhite(s); |
1096 | 5433 |
5434 /* Perl/shell # comment comment continues until eol. Require a space | |
5435 * before # to avoid recognizing $#array. */ | |
5438 | 5436 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') |
1096 | 5437 { |
5438 s += STRLEN(s); | |
5439 break; | |
5440 } | |
7 | 5441 if (*s != '/') |
5442 break; | |
5443 ++s; | |
5444 if (*s == '/') /* slash-slash comment continues till eol */ | |
5445 { | |
5446 s += STRLEN(s); | |
5447 break; | |
5448 } | |
5449 if (*s != '*') | |
5450 break; | |
5451 for (++s; *s; ++s) /* skip slash-star comment */ | |
5452 if (s[0] == '*' && s[1] == '/') | |
5453 { | |
5454 s += 2; | |
5455 break; | |
5456 } | |
5457 } | |
5458 return s; | |
5459 } | |
5460 | |
5461 /* | |
2805 | 5462 * Return TRUE if there is no code at *s. White space and comments are |
7 | 5463 * not considered code. |
5464 */ | |
5465 static int | |
5466 cin_nocode(s) | |
5467 char_u *s; | |
5468 { | |
5469 return *cin_skipcomment(s) == NUL; | |
5470 } | |
5471 | |
5472 /* | |
5473 * Check previous lines for a "//" line comment, skipping over blank lines. | |
5474 */ | |
5475 static pos_T * | |
5476 find_line_comment() /* XXX */ | |
5477 { | |
5478 static pos_T pos; | |
5479 char_u *line; | |
5480 char_u *p; | |
5481 | |
5482 pos = curwin->w_cursor; | |
5483 while (--pos.lnum > 0) | |
5484 { | |
5485 line = ml_get(pos.lnum); | |
5486 p = skipwhite(line); | |
5487 if (cin_islinecomment(p)) | |
5488 { | |
5489 pos.col = (int)(p - line); | |
5490 return &pos; | |
5491 } | |
5492 if (*p != NUL) | |
5493 break; | |
5494 } | |
5495 return NULL; | |
5496 } | |
5497 | |
5498 /* | |
6030 | 5499 * Return TRUE if "text" starts with "key:". |
5500 */ | |
5501 static int | |
5502 cin_has_js_key(text) | |
5503 char_u *text; | |
5504 { | |
5505 char_u *s = skipwhite(text); | |
6104 | 5506 int quote = -1; |
6030 | 5507 |
5508 if (*s == '\'' || *s == '"') | |
5509 { | |
5510 /* can be 'key': or "key": */ | |
5511 quote = *s; | |
5512 ++s; | |
5513 } | |
5514 if (!vim_isIDc(*s)) /* need at least one ID character */ | |
5515 return FALSE; | |
5516 | |
5517 while (vim_isIDc(*s)) | |
5518 ++s; | |
5519 if (*s == quote) | |
5520 ++s; | |
5521 | |
5522 s = cin_skipcomment(s); | |
5523 | |
5524 /* "::" is not a label, it's C++ */ | |
5525 return (*s == ':' && s[1] != ':'); | |
5526 } | |
5527 | |
5528 /* | |
7 | 5529 * Check if string matches "label:"; move to character after ':' if true. |
6030 | 5530 * "*s" must point to the start of the label, if there is one. |
7 | 5531 */ |
5532 static int | |
5533 cin_islabel_skip(s) | |
5534 char_u **s; | |
5535 { | |
5536 if (!vim_isIDc(**s)) /* need at least one ID character */ | |
5537 return FALSE; | |
5538 | |
5539 while (vim_isIDc(**s)) | |
5540 (*s)++; | |
5541 | |
5542 *s = cin_skipcomment(*s); | |
5543 | |
5544 /* "::" is not a label, it's C++ */ | |
5545 return (**s == ':' && *++*s != ':'); | |
5546 } | |
5547 | |
5548 /* | |
5549 * Recognize a label: "label:". | |
5550 * Note: curwin->w_cursor must be where we are looking for the label. | |
5551 */ | |
5552 int | |
5442 | 5553 cin_islabel() /* XXX */ |
7 | 5554 { |
5555 char_u *s; | |
5556 | |
5557 s = cin_skipcomment(ml_get_curline()); | |
5558 | |
5559 /* | |
5560 * Exclude "default" from labels, since it should be indented | |
5561 * like a switch label. Same for C++ scope declarations. | |
5562 */ | |
5563 if (cin_isdefault(s)) | |
5564 return FALSE; | |
5565 if (cin_isscopedecl(s)) | |
5566 return FALSE; | |
5567 | |
5568 if (cin_islabel_skip(&s)) | |
5569 { | |
5570 /* | |
5571 * Only accept a label if the previous line is terminated or is a case | |
5572 * label. | |
5573 */ | |
5574 pos_T cursor_save; | |
5575 pos_T *trypos; | |
5576 char_u *line; | |
5577 | |
5578 cursor_save = curwin->w_cursor; | |
5579 while (curwin->w_cursor.lnum > 1) | |
5580 { | |
5581 --curwin->w_cursor.lnum; | |
5582 | |
5583 /* | |
5584 * If we're in a comment now, skip to the start of the comment. | |
5585 */ | |
5586 curwin->w_cursor.col = 0; | |
5442 | 5587 if ((trypos = ind_find_start_comment()) != NULL) /* XXX */ |
7 | 5588 curwin->w_cursor = *trypos; |
5589 | |
5590 line = ml_get_curline(); | |
5591 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ | |
5592 continue; | |
5593 if (*(line = cin_skipcomment(line)) == NUL) | |
5594 continue; | |
5595 | |
5596 curwin->w_cursor = cursor_save; | |
5597 if (cin_isterminated(line, TRUE, FALSE) | |
5598 || 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
|
5599 || cin_iscase(line, TRUE) |
7 | 5600 || (cin_islabel_skip(&line) && cin_nocode(line))) |
5601 return TRUE; | |
5602 return FALSE; | |
5603 } | |
5604 curwin->w_cursor = cursor_save; | |
5605 return TRUE; /* label at start of file??? */ | |
5606 } | |
5607 return FALSE; | |
5608 } | |
5609 | |
5610 /* | |
4184 | 5611 * Recognize structure initialization and enumerations: |
5612 * "[typedef] [static|public|protected|private] enum" | |
5613 * "[typedef] [static|public|protected|private] = {" | |
7 | 5614 */ |
5615 static int | |
5616 cin_isinit(void) | |
5617 { | |
5618 char_u *s; | |
4184 | 5619 static char *skip[] = {"static", "public", "protected", "private"}; |
7 | 5620 |
5621 s = cin_skipcomment(ml_get_curline()); | |
5622 | |
4184 | 5623 if (cin_starts_with(s, "typedef")) |
7 | 5624 s = cin_skipcomment(s + 7); |
5625 | |
4184 | 5626 for (;;) |
5627 { | |
5628 int i, l; | |
5629 | |
5630 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) | |
5631 { | |
4207 | 5632 l = (int)strlen(skip[i]); |
4184 | 5633 if (cin_starts_with(s, skip[i])) |
5634 { | |
5635 s = cin_skipcomment(s + l); | |
5636 l = 0; | |
5637 break; | |
5638 } | |
5639 } | |
5640 if (l != 0) | |
5641 break; | |
5642 } | |
5643 | |
5644 if (cin_starts_with(s, "enum")) | |
7 | 5645 return TRUE; |
5646 | |
5647 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) | |
5648 return TRUE; | |
5649 | |
5650 return FALSE; | |
5651 } | |
5652 | |
5653 /* | |
5654 * Recognize a switch label: "case .*:" or "default:". | |
5655 */ | |
5656 int | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5657 cin_iscase(s, strict) |
7 | 5658 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
|
5659 int strict; /* Allow relaxed check of case statement for JS */ |
7 | 5660 { |
5661 s = cin_skipcomment(s); | |
4184 | 5662 if (cin_starts_with(s, "case")) |
7 | 5663 { |
5664 for (s += 4; *s; ++s) | |
5665 { | |
5666 s = cin_skipcomment(s); | |
5667 if (*s == ':') | |
5668 { | |
5669 if (s[1] == ':') /* skip over "::" for C++ */ | |
5670 ++s; | |
5671 else | |
5672 return TRUE; | |
5673 } | |
5674 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
|
5675 s += 2; /* skip over ':' */ |
7 | 5676 else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
5677 return FALSE; /* stop at comment */ | |
5678 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
|
5679 { |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5680 /* JS etc. */ |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5681 if (strict) |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5682 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
|
5683 else |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5684 return TRUE; |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5685 } |
7 | 5686 } |
5687 return FALSE; | |
5688 } | |
5689 | |
5690 if (cin_isdefault(s)) | |
5691 return TRUE; | |
5692 return FALSE; | |
5693 } | |
5694 | |
5695 /* | |
5696 * Recognize a "default" switch label. | |
5697 */ | |
5698 static int | |
5699 cin_isdefault(s) | |
5700 char_u *s; | |
5701 { | |
5702 return (STRNCMP(s, "default", 7) == 0 | |
5703 && *(s = cin_skipcomment(s + 7)) == ':' | |
5704 && s[1] != ':'); | |
5705 } | |
5706 | |
5707 /* | |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
5708 * Recognize a "public/private/protected" scope declaration label. |
7 | 5709 */ |
5710 int | |
5711 cin_isscopedecl(s) | |
5712 char_u *s; | |
5713 { | |
5714 int i; | |
5715 | |
5716 s = cin_skipcomment(s); | |
5717 if (STRNCMP(s, "public", 6) == 0) | |
5718 i = 6; | |
5719 else if (STRNCMP(s, "protected", 9) == 0) | |
5720 i = 9; | |
5721 else if (STRNCMP(s, "private", 7) == 0) | |
5722 i = 7; | |
5723 else | |
5724 return FALSE; | |
5725 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); | |
5726 } | |
5727 | |
2857 | 5728 /* Maximum number of lines to search back for a "namespace" line. */ |
5729 #define FIND_NAMESPACE_LIM 20 | |
5730 | |
5731 /* | |
5732 * Recognize a "namespace" scope declaration. | |
5733 */ | |
5734 static int | |
5735 cin_is_cpp_namespace(s) | |
5736 char_u *s; | |
5737 { | |
5738 char_u *p; | |
5739 int has_name = FALSE; | |
5740 | |
5741 s = cin_skipcomment(s); | |
5742 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) | |
5743 { | |
5744 p = cin_skipcomment(skipwhite(s + 9)); | |
5745 while (*p != NUL) | |
5746 { | |
5747 if (vim_iswhite(*p)) | |
5748 { | |
5749 has_name = TRUE; /* found end of a name */ | |
5750 p = cin_skipcomment(skipwhite(p)); | |
5751 } | |
5752 else if (*p == '{') | |
5753 { | |
5754 break; | |
5755 } | |
5756 else if (vim_iswordc(*p)) | |
5757 { | |
5758 if (has_name) | |
5759 return FALSE; /* word character after skipping past name */ | |
5760 ++p; | |
5761 } | |
5762 else | |
5763 { | |
5764 return FALSE; | |
5765 } | |
5766 } | |
5767 return TRUE; | |
5768 } | |
5769 return FALSE; | |
5770 } | |
5771 | |
7 | 5772 /* |
5773 * Return a pointer to the first non-empty non-comment character after a ':'. | |
5774 * Return NULL if not found. | |
5775 * case 234: a = b; | |
5776 * ^ | |
5777 */ | |
5778 static char_u * | |
5779 after_label(l) | |
5780 char_u *l; | |
5781 { | |
5782 for ( ; *l; ++l) | |
5783 { | |
5784 if (*l == ':') | |
5785 { | |
5786 if (l[1] == ':') /* skip over "::" for C++ */ | |
5787 ++l; | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5788 else if (!cin_iscase(l + 1, FALSE)) |
7 | 5789 break; |
5790 } | |
5791 else if (*l == '\'' && l[1] && l[2] == '\'') | |
5792 l += 2; /* skip over 'x' */ | |
5793 } | |
5794 if (*l == NUL) | |
5795 return NULL; | |
5796 l = cin_skipcomment(l + 1); | |
5797 if (*l == NUL) | |
5798 return NULL; | |
5799 return l; | |
5800 } | |
5801 | |
5802 /* | |
5803 * Get indent of line "lnum", skipping a label. | |
5804 * Return 0 if there is nothing after the label. | |
5805 */ | |
5806 static int | |
5807 get_indent_nolabel(lnum) /* XXX */ | |
5808 linenr_T lnum; | |
5809 { | |
5810 char_u *l; | |
5811 pos_T fp; | |
5812 colnr_T col; | |
5813 char_u *p; | |
5814 | |
5815 l = ml_get(lnum); | |
5816 p = after_label(l); | |
5817 if (p == NULL) | |
5818 return 0; | |
5819 | |
5820 fp.col = (colnr_T)(p - l); | |
5821 fp.lnum = lnum; | |
5822 getvcol(curwin, &fp, &col, NULL, NULL); | |
5823 return (int)col; | |
5824 } | |
5825 | |
5826 /* | |
5827 * Find indent for line "lnum", ignoring any case or jump label. | |
829 | 5828 * Also return a pointer to the text (after the label) in "pp". |
7 | 5829 * label: if (asdf && asdfasdf) |
5830 * ^ | |
5831 */ | |
5832 static int | |
5442 | 5833 skip_label(lnum, pp) |
7 | 5834 linenr_T lnum; |
5835 char_u **pp; | |
5836 { | |
5837 char_u *l; | |
5838 int amount; | |
5839 pos_T cursor_save; | |
5840 | |
5841 cursor_save = curwin->w_cursor; | |
5842 curwin->w_cursor.lnum = lnum; | |
5843 l = ml_get_curline(); | |
5844 /* XXX */ | |
5442 | 5845 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel()) |
7 | 5846 { |
5847 amount = get_indent_nolabel(lnum); | |
5848 l = after_label(ml_get_curline()); | |
5849 if (l == NULL) /* just in case */ | |
5850 l = ml_get_curline(); | |
5851 } | |
5852 else | |
5853 { | |
5854 amount = get_indent(); | |
5855 l = ml_get_curline(); | |
5856 } | |
5857 *pp = l; | |
5858 | |
5859 curwin->w_cursor = cursor_save; | |
5860 return amount; | |
5861 } | |
5862 | |
5863 /* | |
5864 * Return the indent of the first variable name after a type in a declaration. | |
5865 * int a, indent of "a" | |
5866 * static struct foo b, indent of "b" | |
5867 * enum bla c, indent of "c" | |
5868 * Returns zero when it doesn't look like a declaration. | |
5869 */ | |
5870 static int | |
5871 cin_first_id_amount() | |
5872 { | |
5873 char_u *line, *p, *s; | |
5874 int len; | |
5875 pos_T fp; | |
5876 colnr_T col; | |
5877 | |
5878 line = ml_get_curline(); | |
5879 p = skipwhite(line); | |
835 | 5880 len = (int)(skiptowhite(p) - p); |
7 | 5881 if (len == 6 && STRNCMP(p, "static", 6) == 0) |
5882 { | |
5883 p = skipwhite(p + 6); | |
856 | 5884 len = (int)(skiptowhite(p) - p); |
7 | 5885 } |
5886 if (len == 6 && STRNCMP(p, "struct", 6) == 0) | |
5887 p = skipwhite(p + 6); | |
5888 else if (len == 4 && STRNCMP(p, "enum", 4) == 0) | |
5889 p = skipwhite(p + 4); | |
5890 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) | |
5891 || (len == 6 && STRNCMP(p, "signed", 6) == 0)) | |
5892 { | |
5893 s = skipwhite(p + len); | |
5894 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3])) | |
5895 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4])) | |
5896 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5])) | |
5897 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4]))) | |
5898 p = s; | |
5899 } | |
5900 for (len = 0; vim_isIDc(p[len]); ++len) | |
5901 ; | |
5902 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p)) | |
5903 return 0; | |
5904 | |
5905 p = skipwhite(p + len); | |
5906 fp.lnum = curwin->w_cursor.lnum; | |
5907 fp.col = (colnr_T)(p - line); | |
5908 getvcol(curwin, &fp, &col, NULL, NULL); | |
5909 return (int)col; | |
5910 } | |
5911 | |
5912 /* | |
5913 * Return the indent of the first non-blank after an equal sign. | |
5914 * char *foo = "here"; | |
5915 * Return zero if no (useful) equal sign found. | |
5916 * Return -1 if the line above "lnum" ends in a backslash. | |
5917 * foo = "asdf\ | |
5918 * asdf\ | |
5919 * here"; | |
5920 */ | |
5921 static int | |
5922 cin_get_equal_amount(lnum) | |
5923 linenr_T lnum; | |
5924 { | |
5925 char_u *line; | |
5926 char_u *s; | |
5927 colnr_T col; | |
5928 pos_T fp; | |
5929 | |
5930 if (lnum > 1) | |
5931 { | |
5932 line = ml_get(lnum - 1); | |
5933 if (*line != NUL && line[STRLEN(line) - 1] == '\\') | |
5934 return -1; | |
5935 } | |
5936 | |
5937 line = s = ml_get(lnum); | |
5938 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) | |
5939 { | |
5940 if (cin_iscomment(s)) /* ignore comments */ | |
5941 s = cin_skipcomment(s); | |
5942 else | |
5943 ++s; | |
5944 } | |
5945 if (*s != '=') | |
5946 return 0; | |
5947 | |
5948 s = skipwhite(s + 1); | |
5949 if (cin_nocode(s)) | |
5950 return 0; | |
5951 | |
5952 if (*s == '"') /* nice alignment for continued strings */ | |
5953 ++s; | |
5954 | |
5955 fp.lnum = lnum; | |
5956 fp.col = (colnr_T)(s - line); | |
5957 getvcol(curwin, &fp, &col, NULL, NULL); | |
5958 return (int)col; | |
5959 } | |
5960 | |
5961 /* | |
5962 * Recognize a preprocessor statement: Any line that starts with '#'. | |
5963 */ | |
5964 static int | |
5965 cin_ispreproc(s) | |
5966 char_u *s; | |
5967 { | |
2776 | 5968 if (*skipwhite(s) == '#') |
7 | 5969 return TRUE; |
5970 return FALSE; | |
5971 } | |
5972 | |
5973 /* | |
5974 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a | |
5975 * continuation line of a preprocessor statement. Decrease "*lnump" to the | |
5976 * start and return the line in "*pp". | |
5977 */ | |
5978 static int | |
5979 cin_ispreproc_cont(pp, lnump) | |
5980 char_u **pp; | |
5981 linenr_T *lnump; | |
5982 { | |
5983 char_u *line = *pp; | |
5984 linenr_T lnum = *lnump; | |
5985 int retval = FALSE; | |
5986 | |
408 | 5987 for (;;) |
7 | 5988 { |
5989 if (cin_ispreproc(line)) | |
5990 { | |
5991 retval = TRUE; | |
5992 *lnump = lnum; | |
5993 break; | |
5994 } | |
5995 if (lnum == 1) | |
5996 break; | |
5997 line = ml_get(--lnum); | |
5998 if (*line == NUL || line[STRLEN(line) - 1] != '\\') | |
5999 break; | |
6000 } | |
6001 | |
6002 if (lnum != *lnump) | |
6003 *pp = ml_get(*lnump); | |
6004 return retval; | |
6005 } | |
6006 | |
6007 /* | |
6008 * Recognize the start of a C or C++ comment. | |
6009 */ | |
6010 static int | |
6011 cin_iscomment(p) | |
6012 char_u *p; | |
6013 { | |
6014 return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); | |
6015 } | |
6016 | |
6017 /* | |
6018 * Recognize the start of a "//" comment. | |
6019 */ | |
6020 static int | |
6021 cin_islinecomment(p) | |
6022 char_u *p; | |
6023 { | |
6024 return (p[0] == '/' && p[1] == '/'); | |
6025 } | |
6026 | |
6027 /* | |
2805 | 6028 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or |
6029 * '}'. | |
7 | 6030 * Don't consider "} else" a terminated line. |
2843 | 6031 * If a line begins with an "else", only consider it terminated if no unmatched |
6032 * opening braces follow (handle "else { foo();" correctly). | |
7 | 6033 * Return the character terminating the line (ending char's have precedence if |
6034 * both apply in order to determine initializations). | |
6035 */ | |
6036 static int | |
6037 cin_isterminated(s, incl_open, incl_comma) | |
6038 char_u *s; | |
6039 int incl_open; /* include '{' at the end as terminator */ | |
6040 int incl_comma; /* recognize a trailing comma */ | |
6041 { | |
2843 | 6042 char_u found_start = 0; |
6043 unsigned n_open = 0; | |
6044 int is_else = FALSE; | |
7 | 6045 |
6046 s = cin_skipcomment(s); | |
6047 | |
6048 if (*s == '{' || (*s == '}' && !cin_iselse(s))) | |
6049 found_start = *s; | |
6050 | |
2843 | 6051 if (!found_start) |
6052 is_else = cin_iselse(s); | |
6053 | |
7 | 6054 while (*s) |
6055 { | |
6056 /* skip over comments, "" strings and 'c'haracters */ | |
6057 s = skip_string(cin_skipcomment(s)); | |
2805 | 6058 if (*s == '}' && n_open > 0) |
6059 --n_open; | |
2843 | 6060 if ((!is_else || n_open == 0) |
2805 | 6061 && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) |
7 | 6062 && cin_nocode(s + 1)) |
6063 return *s; | |
2805 | 6064 else if (*s == '{') |
6065 { | |
6066 if (incl_open && cin_nocode(s + 1)) | |
6067 return *s; | |
6068 else | |
6069 ++n_open; | |
6070 } | |
7 | 6071 |
6072 if (*s) | |
6073 s++; | |
6074 } | |
6075 return found_start; | |
6076 } | |
6077 | |
6078 /* | |
6079 * Recognize the basic picture of a function declaration -- it needs to | |
6080 * have an open paren somewhere and a close paren at the end of the line and | |
6081 * no semicolons anywhere. | |
6082 * When a line ends in a comma we continue looking in the next line. | |
6083 * "sp" points to a string with the line. When looking at other lines it must | |
6084 * be restored to the line. When it's NULL fetch lines here. | |
6085 * "lnum" is where we start looking. | |
3220 | 6086 * "min_lnum" is the line before which we will not be looking. |
7 | 6087 */ |
6088 static int | |
5442 | 6089 cin_isfuncdecl(sp, first_lnum, min_lnum) |
7 | 6090 char_u **sp; |
6091 linenr_T first_lnum; | |
3220 | 6092 linenr_T min_lnum; |
7 | 6093 { |
6094 char_u *s; | |
6095 linenr_T lnum = first_lnum; | |
6096 int retval = FALSE; | |
3220 | 6097 pos_T *trypos; |
6098 int just_started = TRUE; | |
7 | 6099 |
6100 if (sp == NULL) | |
6101 s = ml_get(lnum); | |
6102 else | |
6103 s = *sp; | |
6104 | |
3220 | 6105 if (find_last_paren(s, '(', ')') |
5442 | 6106 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
3220 | 6107 { |
6108 lnum = trypos->lnum; | |
6109 if (lnum < min_lnum) | |
6110 return FALSE; | |
6111 | |
6112 s = ml_get(lnum); | |
6113 } | |
6114 | |
2776 | 6115 /* Ignore line starting with #. */ |
6116 if (cin_ispreproc(s)) | |
6117 return FALSE; | |
6118 | |
7 | 6119 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') |
6120 { | |
6121 if (cin_iscomment(s)) /* ignore comments */ | |
6122 s = cin_skipcomment(s); | |
6123 else | |
6124 ++s; | |
6125 } | |
6126 if (*s != '(') | |
6127 return FALSE; /* ';', ' or " before any () or no '(' */ | |
6128 | |
6129 while (*s && *s != ';' && *s != '\'' && *s != '"') | |
6130 { | |
6131 if (*s == ')' && cin_nocode(s + 1)) | |
6132 { | |
6133 /* ')' at the end: may have found a match | |
6134 * Check for he previous line not to end in a backslash: | |
6135 * #if defined(x) && \ | |
6136 * defined(y) | |
6137 */ | |
6138 lnum = first_lnum - 1; | |
6139 s = ml_get(lnum); | |
6140 if (*s == NUL || s[STRLEN(s) - 1] != '\\') | |
6141 retval = TRUE; | |
6142 goto done; | |
6143 } | |
2776 | 6144 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) |
6145 { | |
6146 int comma = (*s == ','); | |
6147 | |
6148 /* ',' at the end: continue looking in the next line. | |
6149 * At the end: check for ',' in the next line, for this style: | |
6150 * func(arg1 | |
6151 * , arg2) */ | |
6152 for (;;) | |
6153 { | |
6154 if (lnum >= curbuf->b_ml.ml_line_count) | |
6155 break; | |
6156 s = ml_get(++lnum); | |
6157 if (!cin_ispreproc(s)) | |
6158 break; | |
6159 } | |
7 | 6160 if (lnum >= curbuf->b_ml.ml_line_count) |
6161 break; | |
2776 | 6162 /* Require a comma at end of the line or a comma or ')' at the |
6163 * start of next line. */ | |
6164 s = skipwhite(s); | |
3220 | 6165 if (!just_started && (!comma && *s != ',' && *s != ')')) |
2776 | 6166 break; |
3220 | 6167 just_started = FALSE; |
7 | 6168 } |
6169 else if (cin_iscomment(s)) /* ignore comments */ | |
6170 s = cin_skipcomment(s); | |
6171 else | |
3220 | 6172 { |
7 | 6173 ++s; |
3220 | 6174 just_started = FALSE; |
6175 } | |
7 | 6176 } |
6177 | |
6178 done: | |
6179 if (lnum != first_lnum && sp != NULL) | |
6180 *sp = ml_get(first_lnum); | |
6181 | |
6182 return retval; | |
6183 } | |
6184 | |
6185 static int | |
6186 cin_isif(p) | |
6187 char_u *p; | |
6188 { | |
6189 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); | |
6190 } | |
6191 | |
6192 static int | |
6193 cin_iselse(p) | |
6194 char_u *p; | |
6195 { | |
6196 if (*p == '}') /* accept "} else" */ | |
6197 p = cin_skipcomment(p + 1); | |
6198 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); | |
6199 } | |
6200 | |
6201 static int | |
6202 cin_isdo(p) | |
6203 char_u *p; | |
6204 { | |
6205 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); | |
6206 } | |
6207 | |
6208 /* | |
6209 * Check if this is a "while" that should have a matching "do". | |
6210 * We only accept a "while (condition) ;", with only white space between the | |
6211 * ')' and ';'. The condition may be spread over several lines. | |
6212 */ | |
6213 static int | |
5442 | 6214 cin_iswhileofdo(p, lnum) /* XXX */ |
7 | 6215 char_u *p; |
6216 linenr_T lnum; | |
6217 { | |
6218 pos_T cursor_save; | |
6219 pos_T *trypos; | |
6220 int retval = FALSE; | |
6221 | |
6222 p = cin_skipcomment(p); | |
6223 if (*p == '}') /* accept "} while (cond);" */ | |
6224 p = cin_skipcomment(p + 1); | |
4184 | 6225 if (cin_starts_with(p, "while")) |
7 | 6226 { |
6227 cursor_save = curwin->w_cursor; | |
6228 curwin->w_cursor.lnum = lnum; | |
6229 curwin->w_cursor.col = 0; | |
6230 p = ml_get_curline(); | |
6231 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ | |
6232 { | |
6233 ++p; | |
6234 ++curwin->w_cursor.col; | |
6235 } | |
5442 | 6236 if ((trypos = findmatchlimit(NULL, 0, 0, |
6237 curbuf->b_ind_maxparen)) != NULL | |
7 | 6238 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') |
6239 retval = TRUE; | |
6240 curwin->w_cursor = cursor_save; | |
6241 } | |
6242 return retval; | |
6243 } | |
6244 | |
829 | 6245 /* |
3461 | 6246 * Check whether in "p" there is an "if", "for" or "while" before "*poffset". |
3454 | 6247 * Return 0 if there is none. |
6248 * Otherwise return !0 and update "*poffset" to point to the place where the | |
6249 * string was found. | |
6250 */ | |
6251 static int | |
3461 | 6252 cin_is_if_for_while_before_offset(line, poffset) |
3454 | 6253 char_u *line; |
6254 int *poffset; | |
6255 { | |
3461 | 6256 int offset = *poffset; |
3454 | 6257 |
6258 if (offset-- < 2) | |
6259 return 0; | |
6260 while (offset > 2 && vim_iswhite(line[offset])) | |
6261 --offset; | |
6262 | |
6263 offset -= 1; | |
6264 if (!STRNCMP(line + offset, "if", 2)) | |
6265 goto probablyFound; | |
6266 | |
6267 if (offset >= 1) | |
6268 { | |
6269 offset -= 1; | |
6270 if (!STRNCMP(line + offset, "for", 3)) | |
6271 goto probablyFound; | |
6272 | |
6273 if (offset >= 2) | |
6274 { | |
6275 offset -= 2; | |
6276 if (!STRNCMP(line + offset, "while", 5)) | |
6277 goto probablyFound; | |
6278 } | |
6279 } | |
6280 return 0; | |
3461 | 6281 |
3454 | 6282 probablyFound: |
6283 if (!offset || !vim_isIDc(line[offset - 1])) | |
6284 { | |
6285 *poffset = offset; | |
6286 return 1; | |
6287 } | |
6288 return 0; | |
6289 } | |
6290 | |
6291 /* | |
829 | 6292 * Return TRUE if we are at the end of a do-while. |
6293 * do | |
6294 * nothing; | |
6295 * while (foo | |
856 | 6296 * && bar); <-- here |
829 | 6297 * Adjust the cursor to the line with "while". |
6298 */ | |
6299 static int | |
5442 | 6300 cin_iswhileofdo_end(terminated) |
829 | 6301 int terminated; |
6302 { | |
6303 char_u *line; | |
6304 char_u *p; | |
6305 char_u *s; | |
6306 pos_T *trypos; | |
6307 int i; | |
6308 | |
6309 if (terminated != ';') /* there must be a ';' at the end */ | |
6310 return FALSE; | |
6311 | |
6312 p = line = ml_get_curline(); | |
6313 while (*p != NUL) | |
6314 { | |
6315 p = cin_skipcomment(p); | |
6316 if (*p == ')') | |
6317 { | |
6318 s = skipwhite(p + 1); | |
6319 if (*s == ';' && cin_nocode(s + 1)) | |
6320 { | |
6321 /* Found ");" at end of the line, now check there is "while" | |
6322 * before the matching '('. XXX */ | |
835 | 6323 i = (int)(p - line); |
829 | 6324 curwin->w_cursor.col = i; |
5442 | 6325 trypos = find_match_paren(curbuf->b_ind_maxparen); |
829 | 6326 if (trypos != NULL) |
6327 { | |
6328 s = cin_skipcomment(ml_get(trypos->lnum)); | |
6329 if (*s == '}') /* accept "} while (cond);" */ | |
6330 s = cin_skipcomment(s + 1); | |
4184 | 6331 if (cin_starts_with(s, "while")) |
829 | 6332 { |
6333 curwin->w_cursor.lnum = trypos->lnum; | |
6334 return TRUE; | |
6335 } | |
6336 } | |
6337 | |
6338 /* Searching may have made "line" invalid, get it again. */ | |
6339 line = ml_get_curline(); | |
6340 p = line + i; | |
6341 } | |
6342 } | |
6343 if (*p != NUL) | |
6344 ++p; | |
6345 } | |
6346 return FALSE; | |
6347 } | |
6348 | |
7 | 6349 static int |
6350 cin_isbreak(p) | |
6351 char_u *p; | |
6352 { | |
6353 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); | |
6354 } | |
6355 | |
828 | 6356 /* |
6357 * Find the position of a C++ base-class declaration or | |
7 | 6358 * constructor-initialization. eg: |
6359 * | |
6360 * class MyClass : | |
6361 * baseClass <-- here | |
6362 * class MyClass : public baseClass, | |
6363 * anotherBaseClass <-- here (should probably lineup ??) | |
6364 * MyClass::MyClass(...) : | |
6365 * baseClass(...) <-- here (constructor-initialization) | |
827 | 6366 * |
6367 * This is a lot of guessing. Watch out for "cond ? func() : foo". | |
7 | 6368 */ |
6369 static int | |
1336 | 6370 cin_is_cpp_baseclass(col) |
828 | 6371 colnr_T *col; /* return: column to align with */ |
7 | 6372 { |
6373 char_u *s; | |
6374 int class_or_struct, lookfor_ctor_init, cpp_base_class; | |
828 | 6375 linenr_T lnum = curwin->w_cursor.lnum; |
1336 | 6376 char_u *line = ml_get_curline(); |
7 | 6377 |
6378 *col = 0; | |
6379 | |
17 | 6380 s = skipwhite(line); |
6381 if (*s == '#') /* skip #define FOO x ? (x) : x */ | |
6382 return FALSE; | |
6383 s = cin_skipcomment(s); | |
7 | 6384 if (*s == NUL) |
6385 return FALSE; | |
6386 | |
6387 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; | |
6388 | |
828 | 6389 /* Search for a line starting with '#', empty, ending in ';' or containing |
6390 * '{' or '}' and start below it. This handles the following situations: | |
6391 * a = cond ? | |
6392 * func() : | |
856 | 6393 * asdf; |
828 | 6394 * func::foo() |
6395 * : something | |
6396 * {} | |
6397 * Foo::Foo (int one, int two) | |
6398 * : something(4), | |
6399 * somethingelse(3) | |
6400 * {} | |
6401 */ | |
6402 while (lnum > 1) | |
6403 { | |
1336 | 6404 line = ml_get(lnum - 1); |
6405 s = skipwhite(line); | |
828 | 6406 if (*s == '#' || *s == NUL) |
6407 break; | |
6408 while (*s != NUL) | |
6409 { | |
6410 s = cin_skipcomment(s); | |
6411 if (*s == '{' || *s == '}' | |
6412 || (*s == ';' && cin_nocode(s + 1))) | |
6413 break; | |
6414 if (*s != NUL) | |
6415 ++s; | |
6416 } | |
6417 if (*s != NUL) | |
6418 break; | |
6419 --lnum; | |
6420 } | |
6421 | |
1336 | 6422 line = ml_get(lnum); |
6423 s = cin_skipcomment(line); | |
828 | 6424 for (;;) |
6425 { | |
6426 if (*s == NUL) | |
6427 { | |
6428 if (lnum == curwin->w_cursor.lnum) | |
6429 break; | |
6430 /* Continue in the cursor line. */ | |
1336 | 6431 line = ml_get(++lnum); |
6432 s = cin_skipcomment(line); | |
6433 if (*s == NUL) | |
6434 continue; | |
828 | 6435 } |
6436 | |
2807 | 6437 if (s[0] == '"') |
6438 s = skip_string(s) + 1; | |
6439 else if (s[0] == ':') | |
7 | 6440 { |
6441 if (s[1] == ':') | |
6442 { | |
6443 /* skip double colon. It can't be a constructor | |
6444 * initialization any more */ | |
6445 lookfor_ctor_init = FALSE; | |
6446 s = cin_skipcomment(s + 2); | |
6447 } | |
6448 else if (lookfor_ctor_init || class_or_struct) | |
6449 { | |
6450 /* we have something found, that looks like the start of | |
1532 | 6451 * cpp-base-class-declaration or constructor-initialization */ |
7 | 6452 cpp_base_class = TRUE; |
6453 lookfor_ctor_init = class_or_struct = FALSE; | |
6454 *col = 0; | |
6455 s = cin_skipcomment(s + 1); | |
6456 } | |
6457 else | |
6458 s = cin_skipcomment(s + 1); | |
6459 } | |
6460 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) | |
6461 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) | |
6462 { | |
6463 class_or_struct = TRUE; | |
6464 lookfor_ctor_init = FALSE; | |
6465 | |
6466 if (*s == 'c') | |
6467 s = cin_skipcomment(s + 5); | |
6468 else | |
6469 s = cin_skipcomment(s + 6); | |
6470 } | |
6471 else | |
6472 { | |
6473 if (s[0] == '{' || s[0] == '}' || s[0] == ';') | |
6474 { | |
6475 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; | |
6476 } | |
6477 else if (s[0] == ')') | |
6478 { | |
6479 /* Constructor-initialization is assumed if we come across | |
6480 * something like "):" */ | |
6481 class_or_struct = FALSE; | |
6482 lookfor_ctor_init = TRUE; | |
6483 } | |
827 | 6484 else if (s[0] == '?') |
6485 { | |
6486 /* Avoid seeing '() :' after '?' as constructor init. */ | |
6487 return FALSE; | |
6488 } | |
7 | 6489 else if (!vim_isIDc(s[0])) |
6490 { | |
6491 /* if it is not an identifier, we are wrong */ | |
6492 class_or_struct = FALSE; | |
6493 lookfor_ctor_init = FALSE; | |
6494 } | |
6495 else if (*col == 0) | |
6496 { | |
6497 /* it can't be a constructor-initialization any more */ | |
6498 lookfor_ctor_init = FALSE; | |
6499 | |
6500 /* the first statement starts here: lineup with this one... */ | |
828 | 6501 if (cpp_base_class) |
7 | 6502 *col = (colnr_T)(s - line); |
6503 } | |
6504 | |
828 | 6505 /* When the line ends in a comma don't align with it. */ |
6506 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) | |
6507 *col = 0; | |
6508 | |
7 | 6509 s = cin_skipcomment(s + 1); |
6510 } | |
6511 } | |
6512 | |
6513 return cpp_base_class; | |
6514 } | |
6515 | |
828 | 6516 static int |
5442 | 6517 get_baseclass_amount(col) |
828 | 6518 int col; |
6519 { | |
6520 int amount; | |
6521 colnr_T vcol; | |
6522 pos_T *trypos; | |
6523 | |
6524 if (col == 0) | |
6525 { | |
6526 amount = get_indent(); | |
6527 if (find_last_paren(ml_get_curline(), '(', ')') | |
5442 | 6528 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
828 | 6529 amount = get_indent_lnum(trypos->lnum); /* XXX */ |
6530 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) | |
5442 | 6531 amount += curbuf->b_ind_cpp_baseclass; |
828 | 6532 } |
6533 else | |
6534 { | |
6535 curwin->w_cursor.col = col; | |
6536 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); | |
6537 amount = (int)vcol; | |
6538 } | |
5442 | 6539 if (amount < curbuf->b_ind_cpp_baseclass) |
6540 amount = curbuf->b_ind_cpp_baseclass; | |
828 | 6541 return amount; |
6542 } | |
6543 | |
7 | 6544 /* |
6545 * Return TRUE if string "s" ends with the string "find", possibly followed by | |
6546 * white space and comments. Skip strings and comments. | |
6547 * Ignore "ignore" after "find" if it's not NULL. | |
6548 */ | |
6549 static int | |
6550 cin_ends_in(s, find, ignore) | |
6551 char_u *s; | |
6552 char_u *find; | |
6553 char_u *ignore; | |
6554 { | |
6555 char_u *p = s; | |
6556 char_u *r; | |
6557 int len = (int)STRLEN(find); | |
6558 | |
6559 while (*p != NUL) | |
6560 { | |
6561 p = cin_skipcomment(p); | |
6562 if (STRNCMP(p, find, len) == 0) | |
6563 { | |
6564 r = skipwhite(p + len); | |
6565 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) | |
6566 r = skipwhite(r + STRLEN(ignore)); | |
6567 if (cin_nocode(r)) | |
6568 return TRUE; | |
6569 } | |
6570 if (*p != NUL) | |
6571 ++p; | |
6572 } | |
6573 return FALSE; | |
6574 } | |
6575 | |
6576 /* | |
4184 | 6577 * Return TRUE when "s" starts with "word" and then a non-ID character. |
6578 */ | |
6579 static int | |
6580 cin_starts_with(s, word) | |
6581 char_u *s; | |
6582 char *word; | |
6583 { | |
4207 | 6584 int l = (int)STRLEN(word); |
4184 | 6585 |
6586 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); | |
6587 } | |
6588 | |
6589 /* | |
7 | 6590 * Skip strings, chars and comments until at or past "trypos". |
6591 * Return the column found. | |
6592 */ | |
6593 static int | |
6594 cin_skip2pos(trypos) | |
6595 pos_T *trypos; | |
6596 { | |
6597 char_u *line; | |
6598 char_u *p; | |
6599 | |
6600 p = line = ml_get(trypos->lnum); | |
6601 while (*p && (colnr_T)(p - line) < trypos->col) | |
6602 { | |
6603 if (cin_iscomment(p)) | |
6604 p = cin_skipcomment(p); | |
6605 else | |
6606 { | |
6607 p = skip_string(p); | |
6608 ++p; | |
6609 } | |
6610 } | |
6611 return (int)(p - line); | |
6612 } | |
6613 | |
6614 /* | |
6615 * Find the '{' at the start of the block we are in. | |
6616 * Return NULL if no match found. | |
6617 * Ignore a '{' that is in a comment, makes indenting the next three lines | |
6618 * work. */ | |
6619 /* foo() */ | |
6620 /* { */ | |
6621 /* } */ | |
6622 | |
6623 static pos_T * | |
5442 | 6624 find_start_brace() /* XXX */ |
7 | 6625 { |
6626 pos_T cursor_save; | |
6627 pos_T *trypos; | |
6628 pos_T *pos; | |
6629 static pos_T pos_copy; | |
6630 | |
6631 cursor_save = curwin->w_cursor; | |
6632 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) | |
6633 { | |
6634 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ | |
6635 trypos = &pos_copy; | |
6636 curwin->w_cursor = *trypos; | |
6637 pos = NULL; | |
829 | 6638 /* ignore the { if it's in a // or / * * / comment */ |
7 | 6639 if ((colnr_T)cin_skip2pos(trypos) == trypos->col |
5442 | 6640 && (pos = ind_find_start_comment()) == NULL) /* XXX */ |
7 | 6641 break; |
6642 if (pos != NULL) | |
6643 curwin->w_cursor.lnum = pos->lnum; | |
6644 } | |
6645 curwin->w_cursor = cursor_save; | |
6646 return trypos; | |
6647 } | |
6648 | |
6649 /* | |
6020 | 6650 * Find the matching '(', ignoring it if it is in a comment. |
3454 | 6651 * Return NULL if no match found. |
7 | 6652 */ |
6653 static pos_T * | |
5442 | 6654 find_match_paren(ind_maxparen) /* XXX */ |
7 | 6655 int ind_maxparen; |
6656 { | |
6030 | 6657 return find_match_char('(', ind_maxparen); |
6658 } | |
6659 | |
6660 static pos_T * | |
6661 find_match_char(c, ind_maxparen) /* XXX */ | |
6662 int c; | |
6663 int ind_maxparen; | |
6664 { | |
7 | 6665 pos_T cursor_save; |
6666 pos_T *trypos; | |
829 | 6667 static pos_T pos_copy; |
7 | 6668 |
6669 cursor_save = curwin->w_cursor; | |
6030 | 6670 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxparen)) != NULL) |
7 | 6671 { |
6672 /* check if the ( is in a // comment */ | |
6673 if ((colnr_T)cin_skip2pos(trypos) > trypos->col) | |
6674 trypos = NULL; | |
6675 else | |
6676 { | |
6677 pos_copy = *trypos; /* copy trypos, findmatch will change it */ | |
6678 trypos = &pos_copy; | |
6679 curwin->w_cursor = *trypos; | |
5442 | 6680 if (ind_find_start_comment() != NULL) /* XXX */ |
7 | 6681 trypos = NULL; |
6682 } | |
6683 } | |
6684 curwin->w_cursor = cursor_save; | |
6685 return trypos; | |
6686 } | |
6687 | |
6688 /* | |
6020 | 6689 * Find the matching '(', ignoring it if it is in a comment or before an |
6690 * unmatched {. | |
6691 * Return NULL if no match found. | |
6692 */ | |
6693 static pos_T * | |
6694 find_match_paren_after_brace(ind_maxparen) /* XXX */ | |
6695 int ind_maxparen; | |
6696 { | |
6697 pos_T *trypos = find_match_paren(ind_maxparen); | |
6698 | |
6699 if (trypos != NULL) | |
6700 { | |
6701 pos_T *tryposBrace = find_start_brace(); | |
6702 | |
6703 /* If both an unmatched '(' and '{' is found. Ignore the '(' | |
6704 * position if the '{' is further down. */ | |
6705 if (tryposBrace != NULL | |
6706 && (trypos->lnum != tryposBrace->lnum | |
6707 ? trypos->lnum < tryposBrace->lnum | |
6708 : trypos->col < tryposBrace->col)) | |
6709 trypos = NULL; | |
6710 } | |
6711 return trypos; | |
6712 } | |
6713 | |
6714 /* | |
7 | 6715 * Return ind_maxparen corrected for the difference in line number between the |
6716 * cursor position and "startpos". This makes sure that searching for a | |
6717 * matching paren above the cursor line doesn't find a match because of | |
6718 * looking a few lines further. | |
6719 */ | |
6720 static int | |
5442 | 6721 corr_ind_maxparen(startpos) |
7 | 6722 pos_T *startpos; |
6723 { | |
6724 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; | |
6725 | |
5442 | 6726 if (n > 0 && n < curbuf->b_ind_maxparen / 2) |
6727 return curbuf->b_ind_maxparen - (int)n; | |
6728 return curbuf->b_ind_maxparen; | |
7 | 6729 } |
6730 | |
6731 /* | |
6732 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in | |
3172 | 6733 * line "l". "l" must point to the start of the line. |
7 | 6734 */ |
6735 static int | |
6736 find_last_paren(l, start, end) | |
6737 char_u *l; | |
6738 int start, end; | |
6739 { | |
6740 int i; | |
6741 int retval = FALSE; | |
6742 int open_count = 0; | |
6743 | |
6744 curwin->w_cursor.col = 0; /* default is start of line */ | |
6745 | |
3172 | 6746 for (i = 0; l[i] != NUL; i++) |
7 | 6747 { |
6748 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ | |
6749 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ | |
6750 if (l[i] == start) | |
6751 ++open_count; | |
6752 else if (l[i] == end) | |
6753 { | |
6754 if (open_count > 0) | |
6755 --open_count; | |
6756 else | |
6757 { | |
6758 curwin->w_cursor.col = i; | |
6759 retval = TRUE; | |
6760 } | |
6761 } | |
6762 } | |
6763 return retval; | |
6764 } | |
6765 | |
5438 | 6766 /* |
6767 * Parse 'cinoptions' and set the values in "curbuf". | |
6768 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. | |
6769 */ | |
6770 void | |
6771 parse_cino(buf) | |
6772 buf_T *buf; | |
6773 { | |
6774 char_u *p; | |
6775 char_u *l; | |
6776 char_u *digits; | |
6777 int n; | |
6778 int divider; | |
6779 int fraction = 0; | |
6780 int sw = (int)get_sw_value(buf); | |
7 | 6781 |
6782 /* | |
5438 | 6783 * Set the default values. |
7 | 6784 */ |
5438 | 6785 /* Spaces from a block's opening brace the prevailing indent for that |
6786 * block should be. */ | |
6787 buf->b_ind_level = sw; | |
6788 | |
6789 /* Spaces from the edge of the line an open brace that's at the end of a | |
6790 * line is imagined to be. */ | |
6791 buf->b_ind_open_imag = 0; | |
6792 | |
6793 /* Spaces from the prevailing indent for a line that is not preceded by | |
6794 * an opening brace. */ | |
6795 buf->b_ind_no_brace = 0; | |
6796 | |
6797 /* Column where the first { of a function should be located }. */ | |
6798 buf->b_ind_first_open = 0; | |
6799 | |
6800 /* Spaces from the prevailing indent a leftmost open brace should be | |
6801 * located. */ | |
6802 buf->b_ind_open_extra = 0; | |
6803 | |
6804 /* Spaces from the matching open brace (real location for one at the left | |
6805 * edge; imaginary location from one that ends a line) the matching close | |
6806 * brace should be located. */ | |
6807 buf->b_ind_close_extra = 0; | |
6808 | |
6809 /* Spaces from the edge of the line an open brace sitting in the leftmost | |
6810 * column is imagined to be. */ | |
6811 buf->b_ind_open_left_imag = 0; | |
6812 | |
6813 /* Spaces jump labels should be shifted to the left if N is non-negative, | |
6814 * otherwise the jump label will be put to column 1. */ | |
6815 buf->b_ind_jump_label = -1; | |
6816 | |
6817 /* Spaces from the switch() indent a "case xx" label should be located. */ | |
6818 buf->b_ind_case = sw; | |
6819 | |
6820 /* Spaces from the "case xx:" code after a switch() should be located. */ | |
6821 buf->b_ind_case_code = sw; | |
6822 | |
6823 /* Lineup break at end of case in switch() with case label. */ | |
6824 buf->b_ind_case_break = 0; | |
6825 | |
6826 /* Spaces from the class declaration indent a scope declaration label | |
6827 * should be located. */ | |
6828 buf->b_ind_scopedecl = sw; | |
6829 | |
6830 /* Spaces from the scope declaration label code should be located. */ | |
6831 buf->b_ind_scopedecl_code = sw; | |
6832 | |
6833 /* Amount K&R-style parameters should be indented. */ | |
6834 buf->b_ind_param = sw; | |
6835 | |
6836 /* Amount a function type spec should be indented. */ | |
6837 buf->b_ind_func_type = sw; | |
6838 | |
6839 /* Amount a cpp base class declaration or constructor initialization | |
6840 * should be indented. */ | |
6841 buf->b_ind_cpp_baseclass = sw; | |
6842 | |
6843 /* additional spaces beyond the prevailing indent a continuation line | |
6844 * should be located. */ | |
6845 buf->b_ind_continuation = sw; | |
6846 | |
6847 /* Spaces from the indent of the line with an unclosed parentheses. */ | |
6848 buf->b_ind_unclosed = sw * 2; | |
6849 | |
6850 /* Spaces from the indent of the line with an unclosed parentheses, which | |
6851 * itself is also unclosed. */ | |
6852 buf->b_ind_unclosed2 = sw; | |
6853 | |
6854 /* Suppress ignoring spaces from the indent of a line starting with an | |
6855 * unclosed parentheses. */ | |
6856 buf->b_ind_unclosed_noignore = 0; | |
6857 | |
6858 /* If the opening paren is the last nonwhite character on the line, and | |
6859 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer | |
6860 * context (for very long lines). */ | |
6861 buf->b_ind_unclosed_wrapped = 0; | |
6862 | |
6863 /* Suppress ignoring white space when lining up with the character after | |
6864 * an unclosed parentheses. */ | |
6865 buf->b_ind_unclosed_whiteok = 0; | |
6866 | |
6867 /* Indent a closing parentheses under the line start of the matching | |
6868 * opening parentheses. */ | |
6869 buf->b_ind_matching_paren = 0; | |
6870 | |
6871 /* Indent a closing parentheses under the previous line. */ | |
6872 buf->b_ind_paren_prev = 0; | |
6873 | |
6874 /* Extra indent for comments. */ | |
6875 buf->b_ind_comment = 0; | |
6876 | |
6877 /* Spaces from the comment opener when there is nothing after it. */ | |
6878 buf->b_ind_in_comment = 3; | |
6879 | |
6880 /* Boolean: if non-zero, use b_ind_in_comment even if there is something | |
6881 * after the comment opener. */ | |
6882 buf->b_ind_in_comment2 = 0; | |
6883 | |
6884 /* Max lines to search for an open paren. */ | |
6885 buf->b_ind_maxparen = 20; | |
6886 | |
6887 /* Max lines to search for an open comment. */ | |
6888 buf->b_ind_maxcomment = 70; | |
6889 | |
6890 /* Handle braces for java code. */ | |
6891 buf->b_ind_java = 0; | |
6892 | |
6893 /* Not to confuse JS object properties with labels. */ | |
6894 buf->b_ind_js = 0; | |
6895 | |
6896 /* Handle blocked cases correctly. */ | |
6897 buf->b_ind_keep_case_label = 0; | |
6898 | |
6899 /* Handle C++ namespace. */ | |
6900 buf->b_ind_cpp_namespace = 0; | |
6901 | |
6902 /* Handle continuation lines containing conditions of if(), for() and | |
6903 * while(). */ | |
6904 buf->b_ind_if_for_while = 0; | |
6905 | |
6906 for (p = buf->b_p_cino; *p; ) | |
6907 { | |
6908 l = p++; | |
6909 if (*p == '-') | |
6910 ++p; | |
6911 digits = p; /* remember where the digits start */ | |
6912 n = getdigits(&p); | |
6913 divider = 0; | |
6914 if (*p == '.') /* ".5s" means a fraction */ | |
6915 { | |
6916 fraction = atol((char *)++p); | |
6917 while (VIM_ISDIGIT(*p)) | |
6918 { | |
6919 ++p; | |
6920 if (divider) | |
6921 divider *= 10; | |
6922 else | |
6923 divider = 10; | |
6924 } | |
6925 } | |
6926 if (*p == 's') /* "2s" means two times 'shiftwidth' */ | |
6927 { | |
6928 if (p == digits) | |
6929 n = sw; /* just "s" is one 'shiftwidth' */ | |
6930 else | |
6931 { | |
6932 n *= sw; | |
6933 if (divider) | |
6934 n += (sw * fraction + divider / 2) / divider; | |
6935 } | |
6936 ++p; | |
6937 } | |
6938 if (l[1] == '-') | |
6939 n = -n; | |
6940 | |
6941 /* When adding an entry here, also update the default 'cinoptions' in | |
6942 * doc/indent.txt, and add explanation for it! */ | |
6943 switch (*l) | |
6944 { | |
6945 case '>': buf->b_ind_level = n; break; | |
6946 case 'e': buf->b_ind_open_imag = n; break; | |
6947 case 'n': buf->b_ind_no_brace = n; break; | |
6948 case 'f': buf->b_ind_first_open = n; break; | |
6949 case '{': buf->b_ind_open_extra = n; break; | |
6950 case '}': buf->b_ind_close_extra = n; break; | |
6951 case '^': buf->b_ind_open_left_imag = n; break; | |
6952 case 'L': buf->b_ind_jump_label = n; break; | |
6953 case ':': buf->b_ind_case = n; break; | |
6954 case '=': buf->b_ind_case_code = n; break; | |
6955 case 'b': buf->b_ind_case_break = n; break; | |
6956 case 'p': buf->b_ind_param = n; break; | |
6957 case 't': buf->b_ind_func_type = n; break; | |
6958 case '/': buf->b_ind_comment = n; break; | |
6959 case 'c': buf->b_ind_in_comment = n; break; | |
6960 case 'C': buf->b_ind_in_comment2 = n; break; | |
6961 case 'i': buf->b_ind_cpp_baseclass = n; break; | |
6962 case '+': buf->b_ind_continuation = n; break; | |
6963 case '(': buf->b_ind_unclosed = n; break; | |
6964 case 'u': buf->b_ind_unclosed2 = n; break; | |
6965 case 'U': buf->b_ind_unclosed_noignore = n; break; | |
6966 case 'W': buf->b_ind_unclosed_wrapped = n; break; | |
6967 case 'w': buf->b_ind_unclosed_whiteok = n; break; | |
6968 case 'm': buf->b_ind_matching_paren = n; break; | |
6969 case 'M': buf->b_ind_paren_prev = n; break; | |
6970 case ')': buf->b_ind_maxparen = n; break; | |
6971 case '*': buf->b_ind_maxcomment = n; break; | |
6972 case 'g': buf->b_ind_scopedecl = n; break; | |
6973 case 'h': buf->b_ind_scopedecl_code = n; break; | |
6974 case 'j': buf->b_ind_java = n; break; | |
6975 case 'J': buf->b_ind_js = n; break; | |
6976 case 'l': buf->b_ind_keep_case_label = n; break; | |
6977 case '#': buf->b_ind_hash_comment = n; break; | |
6978 case 'N': buf->b_ind_cpp_namespace = n; break; | |
6979 case 'k': buf->b_ind_if_for_while = n; break; | |
6980 } | |
6981 if (*p == ',') | |
6982 ++p; | |
6983 } | |
6984 } | |
6985 | |
6986 int | |
6987 get_c_indent() | |
6988 { | |
7 | 6989 pos_T cur_curpos; |
6990 int amount; | |
6991 int scope_amount; | |
834 | 6992 int cur_amount = MAXCOL; |
7 | 6993 colnr_T col; |
6994 char_u *theline; | |
6995 char_u *linecopy; | |
6996 pos_T *trypos; | |
6997 pos_T *tryposBrace = NULL; | |
6998 pos_T our_paren_pos; | |
6999 char_u *start; | |
7000 int start_brace; | |
1532 | 7001 #define BRACE_IN_COL0 1 /* '{' is in column 0 */ |
7 | 7002 #define BRACE_AT_START 2 /* '{' is at start of line */ |
7003 #define BRACE_AT_END 3 /* '{' is at end of line */ | |
7004 linenr_T ourscope; | |
7005 char_u *l; | |
7006 char_u *look; | |
7007 char_u terminated; | |
7008 int lookfor; | |
7009 #define LOOKFOR_INITIAL 0 | |
7010 #define LOOKFOR_IF 1 | |
7011 #define LOOKFOR_DO 2 | |
7012 #define LOOKFOR_CASE 3 | |
7013 #define LOOKFOR_ANY 4 | |
7014 #define LOOKFOR_TERM 5 | |
7015 #define LOOKFOR_UNTERM 6 | |
7016 #define LOOKFOR_SCOPEDECL 7 | |
7017 #define LOOKFOR_NOBREAK 8 | |
7018 #define LOOKFOR_CPP_BASECLASS 9 | |
7019 #define LOOKFOR_ENUM_OR_INIT 10 | |
6030 | 7020 #define LOOKFOR_JS_KEY 11 |
7021 #define LOOKFOR_NO_COMMA 12 | |
7 | 7022 |
7023 int whilelevel; | |
7024 linenr_T lnum; | |
7025 int n; | |
7026 int iscase; | |
7027 int lookfor_break; | |
2857 | 7028 int lookfor_cpp_namespace = FALSE; |
7 | 7029 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
|
7030 int original_line_islabel; |
3123 | 7031 int added_to_amount = 0; |
6030 | 7032 int js_cur_has_key = 0; |
7 | 7033 |
5438 | 7034 /* make a copy, value is changed below */ |
7035 int ind_continuation = curbuf->b_ind_continuation; | |
7 | 7036 |
7037 /* remember where the cursor was when we started */ | |
7038 cur_curpos = curwin->w_cursor; | |
7039 | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7040 /* 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
|
7041 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
|
7042 return 0; |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7043 |
7 | 7044 /* Get a copy of the current contents of the line. |
7045 * This is required, because only the most recent line obtained with | |
7046 * ml_get is valid! */ | |
7047 linecopy = vim_strsave(ml_get(cur_curpos.lnum)); | |
7048 if (linecopy == NULL) | |
7049 return 0; | |
7050 | |
7051 /* | |
7052 * In insert mode and the cursor is on a ')' truncate the line at the | |
7053 * cursor position. We don't want to line up with the matching '(' when | |
7054 * inserting new stuff. | |
7055 * For unknown reasons the cursor might be past the end of the line, thus | |
7056 * check for that. | |
7057 */ | |
7058 if ((State & INSERT) | |
1883 | 7059 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
7 | 7060 && linecopy[curwin->w_cursor.col] == ')') |
7061 linecopy[curwin->w_cursor.col] = NUL; | |
7062 | |
7063 theline = skipwhite(linecopy); | |
7064 | |
7065 /* move the cursor to the start of the line */ | |
7066 | |
7067 curwin->w_cursor.col = 0; | |
7068 | |
5442 | 7069 original_line_islabel = cin_islabel(); /* XXX */ |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
7070 |
7 | 7071 /* |
7072 * #defines and so on always go at the left when included in 'cinkeys'. | |
7073 */ | |
7074 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) | |
5438 | 7075 amount = curbuf->b_ind_hash_comment; |
7 | 7076 |
7077 /* | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
7078 * 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
|
7079 * - JS flag is set. |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
7080 * - 'L' item has a positive value. |
7 | 7081 */ |
5438 | 7082 else if (original_line_islabel && !curbuf->b_ind_js |
7083 && curbuf->b_ind_jump_label < 0) | |
7 | 7084 { |
7085 amount = 0; | |
7086 } | |
7087 | |
7088 /* | |
7089 * If we're inside a "//" comment and there is a "//" comment in a | |
7090 * previous line, lineup with that one. | |
7091 */ | |
7092 else if (cin_islinecomment(theline) | |
7093 && (trypos = find_line_comment()) != NULL) /* XXX */ | |
7094 { | |
7095 /* find how indented the line beginning the comment is */ | |
7096 getvcol(curwin, trypos, &col, NULL, NULL); | |
7097 amount = col; | |
7098 } | |
7099 | |
7100 /* | |
7101 * If we're inside a comment and not looking at the start of the | |
7102 * comment, try using the 'comments' option. | |
7103 */ | |
7104 else if (!cin_iscomment(theline) | |
5442 | 7105 && (trypos = ind_find_start_comment()) != NULL) |
5438 | 7106 /* XXX */ |
7 | 7107 { |
7108 int lead_start_len = 2; | |
7109 int lead_middle_len = 1; | |
7110 char_u lead_start[COM_MAX_LEN]; /* start-comment string */ | |
7111 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ | |
7112 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ | |
7113 char_u *p; | |
7114 int start_align = 0; | |
7115 int start_off = 0; | |
7116 int done = FALSE; | |
7117 | |
7118 /* find how indented the line beginning the comment is */ | |
7119 getvcol(curwin, trypos, &col, NULL, NULL); | |
7120 amount = col; | |
2764 | 7121 *lead_start = NUL; |
7122 *lead_middle = NUL; | |
7 | 7123 |
7124 p = curbuf->b_p_com; | |
7125 while (*p != NUL) | |
7126 { | |
7127 int align = 0; | |
7128 int off = 0; | |
7129 int what = 0; | |
7130 | |
7131 while (*p != NUL && *p != ':') | |
7132 { | |
7133 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) | |
7134 what = *p++; | |
7135 else if (*p == COM_LEFT || *p == COM_RIGHT) | |
7136 align = *p++; | |
7137 else if (VIM_ISDIGIT(*p) || *p == '-') | |
7138 off = getdigits(&p); | |
7139 else | |
7140 ++p; | |
7141 } | |
7142 | |
7143 if (*p == ':') | |
7144 ++p; | |
7145 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
7146 if (what == COM_START) | |
7147 { | |
7148 STRCPY(lead_start, lead_end); | |
7149 lead_start_len = (int)STRLEN(lead_start); | |
7150 start_off = off; | |
7151 start_align = align; | |
7152 } | |
7153 else if (what == COM_MIDDLE) | |
7154 { | |
7155 STRCPY(lead_middle, lead_end); | |
7156 lead_middle_len = (int)STRLEN(lead_middle); | |
7157 } | |
7158 else if (what == COM_END) | |
7159 { | |
7160 /* If our line starts with the middle comment string, line it | |
7161 * up with the comment opener per the 'comments' option. */ | |
7162 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 | |
7163 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) | |
7164 { | |
7165 done = TRUE; | |
7166 if (curwin->w_cursor.lnum > 1) | |
7167 { | |
7168 /* If the start comment string matches in the previous | |
1532 | 7169 * line, use the indent of that line plus offset. If |
7 | 7170 * the middle comment string matches in the previous |
7171 * line, use the indent of that line. XXX */ | |
7172 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); | |
7173 if (STRNCMP(look, lead_start, lead_start_len) == 0) | |
7174 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
7175 else if (STRNCMP(look, lead_middle, | |
7176 lead_middle_len) == 0) | |
7177 { | |
7178 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
7179 break; | |
7180 } | |
7181 /* If the start comment string doesn't match with the | |
7182 * start of the comment, skip this entry. XXX */ | |
7183 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col, | |
7184 lead_start, lead_start_len) != 0) | |
7185 continue; | |
7186 } | |
7187 if (start_off != 0) | |
7188 amount += start_off; | |
7189 else if (start_align == COM_RIGHT) | |
17 | 7190 amount += vim_strsize(lead_start) |
7191 - vim_strsize(lead_middle); | |
7 | 7192 break; |
7193 } | |
7194 | |
7195 /* If our line starts with the end comment string, line it up | |
7196 * with the middle comment */ | |
7197 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 | |
7198 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) | |
7199 { | |
7200 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
7201 /* XXX */ | |
7202 if (off != 0) | |
7203 amount += off; | |
7204 else if (align == COM_RIGHT) | |
17 | 7205 amount += vim_strsize(lead_start) |
7206 - vim_strsize(lead_middle); | |
7 | 7207 done = TRUE; |
7208 break; | |
7209 } | |
7210 } | |
7211 } | |
7212 | |
7213 /* If our line starts with an asterisk, line up with the | |
7214 * asterisk in the comment opener; otherwise, line up | |
7215 * with the first character of the comment text. | |
7216 */ | |
7217 if (done) | |
7218 ; | |
7219 else if (theline[0] == '*') | |
7220 amount += 1; | |
7221 else | |
7222 { | |
7223 /* | |
7224 * If we are more than one line away from the comment opener, take | |
7225 * the indent of the previous non-empty line. If 'cino' has "CO" | |
7226 * and we are just below the comment opener and there are any | |
7227 * white characters after it line up with the text after it; | |
7228 * otherwise, add the amount specified by "c" in 'cino' | |
7229 */ | |
7230 amount = -1; | |
7231 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum) | |
7232 { | |
7233 if (linewhite(lnum)) /* skip blank lines */ | |
7234 continue; | |
7235 amount = get_indent_lnum(lnum); /* XXX */ | |
7236 break; | |
7237 } | |
7238 if (amount == -1) /* use the comment opener */ | |
7239 { | |
5438 | 7240 if (!curbuf->b_ind_in_comment2) |
7 | 7241 { |
7242 start = ml_get(trypos->lnum); | |
7243 look = start + trypos->col + 2; /* skip / and * */ | |
7244 if (*look != NUL) /* if something after it */ | |
7245 trypos->col = (colnr_T)(skipwhite(look) - start); | |
7246 } | |
7247 getvcol(curwin, trypos, &col, NULL, NULL); | |
7248 amount = col; | |
5438 | 7249 if (curbuf->b_ind_in_comment2 || *look == NUL) |
7250 amount += curbuf->b_ind_in_comment; | |
7 | 7251 } |
7252 } | |
7253 } | |
7254 | |
7255 /* | |
6030 | 7256 * Are we looking at a ']' that has a match? |
7257 */ | |
7258 else if (*skipwhite(theline) == ']' | |
7259 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) | |
7260 { | |
7261 /* align with the line containing the '['. */ | |
7262 amount = get_indent_lnum(trypos->lnum); | |
7263 } | |
7264 | |
7265 /* | |
7 | 7266 * Are we inside parentheses or braces? |
7267 */ /* XXX */ | |
5442 | 7268 else if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL |
5438 | 7269 && curbuf->b_ind_java == 0) |
5442 | 7270 || (tryposBrace = find_start_brace()) != NULL |
7 | 7271 || trypos != NULL) |
7272 { | |
7273 if (trypos != NULL && tryposBrace != NULL) | |
7274 { | |
7275 /* Both an unmatched '(' and '{' is found. Use the one which is | |
7276 * closer to the current cursor position, set the other to NULL. */ | |
7277 if (trypos->lnum != tryposBrace->lnum | |
7278 ? trypos->lnum < tryposBrace->lnum | |
7279 : trypos->col < tryposBrace->col) | |
7280 trypos = NULL; | |
7281 else | |
7282 tryposBrace = NULL; | |
7283 } | |
7284 | |
7285 if (trypos != NULL) | |
7286 { | |
7287 /* | |
7288 * If the matching paren is more than one line away, use the indent of | |
7289 * a previous non-empty line that matches the same paren. | |
7290 */ | |
5438 | 7291 if (theline[0] == ')' && curbuf->b_ind_paren_prev) |
829 | 7292 { |
7293 /* Line up with the start of the matching paren line. */ | |
7294 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */ | |
7295 } | |
7296 else | |
7297 { | |
7298 amount = -1; | |
7299 our_paren_pos = *trypos; | |
7300 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) | |
7 | 7301 { |
829 | 7302 l = skipwhite(ml_get(lnum)); |
7303 if (cin_nocode(l)) /* skip comment lines */ | |
7304 continue; | |
7305 if (cin_ispreproc_cont(&l, &lnum)) | |
7306 continue; /* ignore #define, #if, etc. */ | |
7307 curwin->w_cursor.lnum = lnum; | |
7308 | |
7309 /* Skip a comment. XXX */ | |
5442 | 7310 if ((trypos = ind_find_start_comment()) != NULL) |
829 | 7311 { |
7312 lnum = trypos->lnum + 1; | |
7313 continue; | |
7314 } | |
7315 | |
7316 /* XXX */ | |
7317 if ((trypos = find_match_paren( | |
5442 | 7318 corr_ind_maxparen(&cur_curpos))) != NULL |
829 | 7319 && trypos->lnum == our_paren_pos.lnum |
7320 && trypos->col == our_paren_pos.col) | |
7321 { | |
7322 amount = get_indent_lnum(lnum); /* XXX */ | |
7323 | |
7324 if (theline[0] == ')') | |
7325 { | |
7326 if (our_paren_pos.lnum != lnum | |
7327 && cur_amount > amount) | |
7328 cur_amount = amount; | |
7329 amount = -1; | |
7330 } | |
7331 break; | |
7332 } | |
7 | 7333 } |
7334 } | |
7335 | |
7336 /* | |
7337 * Line up with line where the matching paren is. XXX | |
7338 * If the line starts with a '(' or the indent for unclosed | |
7339 * parentheses is zero, line up with the unclosed parentheses. | |
7340 */ | |
7341 if (amount == -1) | |
7342 { | |
829 | 7343 int ignore_paren_col = 0; |
3454 | 7344 int is_if_for_while = 0; |
7345 | |
5438 | 7346 if (curbuf->b_ind_if_for_while) |
3454 | 7347 { |
7348 /* Look for the outermost opening parenthesis on this line | |
7349 * and check whether it belongs to an "if", "for" or "while". */ | |
7350 | |
7351 pos_T cursor_save = curwin->w_cursor; | |
7352 pos_T outermost; | |
7353 char_u *line; | |
7354 | |
7355 trypos = &our_paren_pos; | |
7356 do { | |
7357 outermost = *trypos; | |
7358 curwin->w_cursor.lnum = outermost.lnum; | |
7359 curwin->w_cursor.col = outermost.col; | |
7360 | |
5442 | 7361 trypos = find_match_paren(curbuf->b_ind_maxparen); |
3454 | 7362 } while (trypos && trypos->lnum == outermost.lnum); |
7363 | |
7364 curwin->w_cursor = cursor_save; | |
7365 | |
7366 line = ml_get(outermost.lnum); | |
7367 | |
7368 is_if_for_while = | |
3461 | 7369 cin_is_if_for_while_before_offset(line, &outermost.col); |
3454 | 7370 } |
829 | 7371 |
5442 | 7372 amount = skip_label(our_paren_pos.lnum, &look); |
829 | 7373 look = skipwhite(look); |
7374 if (*look == '(') | |
7375 { | |
7376 linenr_T save_lnum = curwin->w_cursor.lnum; | |
7377 char_u *line; | |
7378 int look_col; | |
7379 | |
7380 /* Ignore a '(' in front of the line that has a match before | |
7381 * our matching '('. */ | |
7382 curwin->w_cursor.lnum = our_paren_pos.lnum; | |
7383 line = ml_get_curline(); | |
835 | 7384 look_col = (int)(look - line); |
829 | 7385 curwin->w_cursor.col = look_col + 1; |
5438 | 7386 if ((trypos = findmatchlimit(NULL, ')', 0, |
7387 curbuf->b_ind_maxparen)) | |
829 | 7388 != NULL |
7389 && trypos->lnum == our_paren_pos.lnum | |
7390 && trypos->col < our_paren_pos.col) | |
7391 ignore_paren_col = trypos->col + 1; | |
7392 | |
7393 curwin->w_cursor.lnum = save_lnum; | |
7394 look = ml_get(our_paren_pos.lnum) + look_col; | |
7395 } | |
5438 | 7396 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0 |
7397 && is_if_for_while == 0) | |
7398 || (!curbuf->b_ind_unclosed_noignore && *look == '(' | |
829 | 7399 && ignore_paren_col == 0)) |
7 | 7400 { |
7401 /* | |
7402 * If we're looking at a close paren, line up right there; | |
7403 * otherwise, line up with the next (non-white) character. | |
5438 | 7404 * When b_ind_unclosed_wrapped is set and the matching paren is |
7 | 7405 * the last nonwhite character of the line, use either the |
7406 * indent of the current line or the indentation of the next | |
5438 | 7407 * outer paren and add b_ind_unclosed_wrapped (for very long |
7 | 7408 * lines). |
7409 */ | |
7410 if (theline[0] != ')') | |
7411 { | |
7412 cur_amount = MAXCOL; | |
7413 l = ml_get(our_paren_pos.lnum); | |
5438 | 7414 if (curbuf->b_ind_unclosed_wrapped |
7 | 7415 && cin_ends_in(l, (char_u *)"(", NULL)) |
7416 { | |
7417 /* look for opening unmatched paren, indent one level | |
7418 * for each additional level */ | |
7419 n = 1; | |
7420 for (col = 0; col < our_paren_pos.col; ++col) | |
7421 { | |
7422 switch (l[col]) | |
7423 { | |
7424 case '(': | |
7425 case '{': ++n; | |
7426 break; | |
7427 | |
7428 case ')': | |
7429 case '}': if (n > 1) | |
7430 --n; | |
7431 break; | |
7432 } | |
7433 } | |
7434 | |
7435 our_paren_pos.col = 0; | |
5438 | 7436 amount += n * curbuf->b_ind_unclosed_wrapped; |
7 | 7437 } |
5438 | 7438 else if (curbuf->b_ind_unclosed_whiteok) |
7 | 7439 our_paren_pos.col++; |
7440 else | |
7441 { | |
7442 col = our_paren_pos.col + 1; | |
7443 while (vim_iswhite(l[col])) | |
7444 col++; | |
7445 if (l[col] != NUL) /* In case of trailing space */ | |
7446 our_paren_pos.col = col; | |
7447 else | |
7448 our_paren_pos.col++; | |
7449 } | |
7450 } | |
7451 | |
7452 /* | |
7453 * Find how indented the paren is, or the character after it | |
7454 * if we did the above "if". | |
7455 */ | |
7456 if (our_paren_pos.col > 0) | |
7457 { | |
7458 getvcol(curwin, &our_paren_pos, &col, NULL, NULL); | |
7459 if (cur_amount > (int)col) | |
7460 cur_amount = col; | |
7461 } | |
7462 } | |
7463 | |
5438 | 7464 if (theline[0] == ')' && curbuf->b_ind_matching_paren) |
7 | 7465 { |
7466 /* Line up with the start of the matching paren line. */ | |
7467 } | |
5438 | 7468 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0) |
7469 || (!curbuf->b_ind_unclosed_noignore | |
829 | 7470 && *look == '(' && ignore_paren_col == 0)) |
7 | 7471 { |
7472 if (cur_amount != MAXCOL) | |
7473 amount = cur_amount; | |
7474 } | |
7475 else | |
7476 { | |
5438 | 7477 /* Add b_ind_unclosed2 for each '(' before our matching one, |
7478 * but ignore (void) before the line (ignore_paren_col). */ | |
7 | 7479 col = our_paren_pos.col; |
834 | 7480 while ((int)our_paren_pos.col > ignore_paren_col) |
7 | 7481 { |
7482 --our_paren_pos.col; | |
7483 switch (*ml_get_pos(&our_paren_pos)) | |
7484 { | |
5438 | 7485 case '(': amount += curbuf->b_ind_unclosed2; |
7 | 7486 col = our_paren_pos.col; |
7487 break; | |
5438 | 7488 case ')': amount -= curbuf->b_ind_unclosed2; |
7 | 7489 col = MAXCOL; |
7490 break; | |
7491 } | |
7492 } | |
7493 | |
5438 | 7494 /* Use b_ind_unclosed once, when the first '(' is not inside |
7 | 7495 * braces */ |
7496 if (col == MAXCOL) | |
5438 | 7497 amount += curbuf->b_ind_unclosed; |
7 | 7498 else |
7499 { | |
7500 curwin->w_cursor.lnum = our_paren_pos.lnum; | |
7501 curwin->w_cursor.col = col; | |
6020 | 7502 if (find_match_paren_after_brace(curbuf->b_ind_maxparen) |
7503 != NULL) | |
5438 | 7504 amount += curbuf->b_ind_unclosed2; |
7 | 7505 else |
3454 | 7506 { |
7507 if (is_if_for_while) | |
5438 | 7508 amount += curbuf->b_ind_if_for_while; |
3454 | 7509 else |
5438 | 7510 amount += curbuf->b_ind_unclosed; |
3454 | 7511 } |
7 | 7512 } |
7513 /* | |
7514 * For a line starting with ')' use the minimum of the two | |
7515 * positions, to avoid giving it more indent than the previous | |
7516 * lines: | |
7517 * func_long_name( if (x | |
7518 * arg && yy | |
7519 * ) ^ not here ) ^ not here | |
7520 */ | |
7521 if (cur_amount < amount) | |
7522 amount = cur_amount; | |
7523 } | |
7524 } | |
7525 | |
7526 /* add extra indent for a comment */ | |
7527 if (cin_iscomment(theline)) | |
5438 | 7528 amount += curbuf->b_ind_comment; |
7 | 7529 } |
7530 else | |
7531 { | |
6030 | 7532 /* |
7533 * We are inside braces, there is a { before this line at the position | |
7534 * stored in tryposBrace. | |
7535 */ | |
7 | 7536 trypos = tryposBrace; |
7537 ourscope = trypos->lnum; | |
7538 start = ml_get(ourscope); | |
7539 | |
7540 /* | |
7541 * Now figure out how indented the line is in general. | |
7542 * If the brace was at the start of the line, we use that; | |
7543 * otherwise, check out the indentation of the line as | |
7544 * a whole and then add the "imaginary indent" to that. | |
7545 */ | |
7546 look = skipwhite(start); | |
7547 if (*look == '{') | |
7548 { | |
7549 getvcol(curwin, trypos, &col, NULL, NULL); | |
7550 amount = col; | |
7551 if (*start == '{') | |
7552 start_brace = BRACE_IN_COL0; | |
7553 else | |
7554 start_brace = BRACE_AT_START; | |
7555 } | |
7556 else | |
7557 { | |
6030 | 7558 /* That opening brace might have been on a continuation |
7559 * line. if so, find the start of the line. */ | |
7 | 7560 curwin->w_cursor.lnum = ourscope; |
7561 | |
6030 | 7562 /* Position the cursor over the rightmost paren, so that |
7563 * matching it will take us back to the start of the line. */ | |
7 | 7564 lnum = ourscope; |
7565 if (find_last_paren(start, '(', ')') | |
5442 | 7566 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) |
7567 != NULL) | |
7 | 7568 lnum = trypos->lnum; |
7569 | |
6030 | 7570 /* It could have been something like |
7 | 7571 * case 1: if (asdf && |
7572 * ldfd) { | |
7573 * } | |
7574 */ | |
6016 | 7575 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) |
7576 && cin_iscase(skipwhite(ml_get_curline()), FALSE)) | |
7 | 7577 amount = get_indent(); |
6016 | 7578 else if (curbuf->b_ind_js) |
7579 amount = get_indent_lnum(lnum); | |
7 | 7580 else |
5442 | 7581 amount = skip_label(lnum, &l); |
7 | 7582 |
7583 start_brace = BRACE_AT_END; | |
7584 } | |
7585 | |
6030 | 7586 /* For Javascript check if the line starts with "key:". */ |
7587 if (curbuf->b_ind_js) | |
7588 js_cur_has_key = cin_has_js_key(theline); | |
7589 | |
7 | 7590 /* |
6030 | 7591 * If we're looking at a closing brace, that's where |
7 | 7592 * we want to be. otherwise, add the amount of room |
7593 * that an indent is supposed to be. | |
7594 */ | |
7595 if (theline[0] == '}') | |
7596 { | |
7597 /* | |
7598 * they may want closing braces to line up with something | |
7599 * other than the open brace. indulge them, if so. | |
7600 */ | |
5438 | 7601 amount += curbuf->b_ind_close_extra; |
7 | 7602 } |
7603 else | |
7604 { | |
7605 /* | |
7606 * If we're looking at an "else", try to find an "if" | |
7607 * to match it with. | |
7608 * If we're looking at a "while", try to find a "do" | |
7609 * to match it with. | |
7610 */ | |
7611 lookfor = LOOKFOR_INITIAL; | |
7612 if (cin_iselse(theline)) | |
7613 lookfor = LOOKFOR_IF; | |
5442 | 7614 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */ |
7 | 7615 lookfor = LOOKFOR_DO; |
7616 if (lookfor != LOOKFOR_INITIAL) | |
7617 { | |
7618 curwin->w_cursor.lnum = cur_curpos.lnum; | |
5442 | 7619 if (find_match(lookfor, ourscope) == OK) |
7 | 7620 { |
7621 amount = get_indent(); /* XXX */ | |
7622 goto theend; | |
7623 } | |
7624 } | |
7625 | |
7626 /* | |
7627 * We get here if we are not on an "while-of-do" or "else" (or | |
7628 * failed to find a matching "if"). | |
7629 * Search backwards for something to line up with. | |
7630 * First set amount for when we don't find anything. | |
7631 */ | |
7632 | |
7633 /* | |
7634 * if the '{' is _really_ at the left margin, use the imaginary | |
7635 * location of a left-margin brace. Otherwise, correct the | |
5438 | 7636 * location for b_ind_open_extra. |
7 | 7637 */ |
7638 | |
7639 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ | |
7640 { | |
5438 | 7641 amount = curbuf->b_ind_open_left_imag; |
2857 | 7642 lookfor_cpp_namespace = TRUE; |
7643 } | |
7644 else if (start_brace == BRACE_AT_START && | |
7645 lookfor_cpp_namespace) /* '{' is at start */ | |
7646 { | |
7647 | |
7648 lookfor_cpp_namespace = TRUE; | |
7 | 7649 } |
7650 else | |
7651 { | |
7652 if (start_brace == BRACE_AT_END) /* '{' is at end of line */ | |
2857 | 7653 { |
5438 | 7654 amount += curbuf->b_ind_open_imag; |
2857 | 7655 |
7656 l = skipwhite(ml_get_curline()); | |
7657 if (cin_is_cpp_namespace(l)) | |
5438 | 7658 amount += curbuf->b_ind_cpp_namespace; |
2857 | 7659 } |
7 | 7660 else |
7661 { | |
5438 | 7662 /* Compensate for adding b_ind_open_extra later. */ |
7663 amount -= curbuf->b_ind_open_extra; | |
7 | 7664 if (amount < 0) |
7665 amount = 0; | |
7666 } | |
7667 } | |
7668 | |
7669 lookfor_break = FALSE; | |
7670 | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7671 if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
7 | 7672 { |
7673 lookfor = LOOKFOR_CASE; /* find a previous switch() label */ | |
5438 | 7674 amount += curbuf->b_ind_case; |
7 | 7675 } |
7676 else if (cin_isscopedecl(theline)) /* private:, ... */ | |
7677 { | |
7678 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ | |
5438 | 7679 amount += curbuf->b_ind_scopedecl; |
7 | 7680 } |
7681 else | |
7682 { | |
5438 | 7683 if (curbuf->b_ind_case_break && cin_isbreak(theline)) |
7684 /* break; ... */ | |
7 | 7685 lookfor_break = TRUE; |
7686 | |
7687 lookfor = LOOKFOR_INITIAL; | |
5438 | 7688 /* b_ind_level from start of block */ |
7689 amount += curbuf->b_ind_level; | |
7 | 7690 } |
7691 scope_amount = amount; | |
7692 whilelevel = 0; | |
7693 | |
7694 /* | |
7695 * Search backwards. If we find something we recognize, line up | |
7696 * with that. | |
7697 * | |
6030 | 7698 * If we're looking at an open brace, indent |
7 | 7699 * the usual amount relative to the conditional |
7700 * that opens the block. | |
7701 */ | |
7702 curwin->w_cursor = cur_curpos; | |
7703 for (;;) | |
7704 { | |
7705 curwin->w_cursor.lnum--; | |
7706 curwin->w_cursor.col = 0; | |
7707 | |
7708 /* | |
7709 * If we went all the way back to the start of our scope, line | |
7710 * up with it. | |
7711 */ | |
7712 if (curwin->w_cursor.lnum <= ourscope) | |
7713 { | |
7714 /* we reached end of scope: | |
7715 * if looking for a enum or structure initialization | |
7716 * go further back: | |
7717 * if it is an initializer (enum xxx or xxx =), then | |
7718 * don't add ind_continuation, otherwise it is a variable | |
7719 * declaration: | |
7720 * int x, | |
7721 * here; <-- add ind_continuation | |
7722 */ | |
7723 if (lookfor == LOOKFOR_ENUM_OR_INIT) | |
7724 { | |
7725 if (curwin->w_cursor.lnum == 0 | |
7726 || curwin->w_cursor.lnum | |
5438 | 7727 < ourscope - curbuf->b_ind_maxparen) |
7 | 7728 { |
5438 | 7729 /* nothing found (abuse curbuf->b_ind_maxparen as |
7730 * limit) assume terminated line (i.e. a variable | |
7 | 7731 * initialization) */ |
7732 if (cont_amount > 0) | |
7733 amount = cont_amount; | |
5438 | 7734 else if (!curbuf->b_ind_js) |
7 | 7735 amount += ind_continuation; |
7736 break; | |
7737 } | |
7738 | |
7739 l = ml_get_curline(); | |
7740 | |
7741 /* | |
7742 * If we're in a comment now, skip to the start of the | |
7743 * comment. | |
7744 */ | |
5442 | 7745 trypos = ind_find_start_comment(); |
7 | 7746 if (trypos != NULL) |
7747 { | |
7748 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7749 curwin->w_cursor.col = 0; |
7 | 7750 continue; |
7751 } | |
7752 | |
7753 /* | |
7754 * Skip preprocessor directives and blank lines. | |
7755 */ | |
7756 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) | |
7757 continue; | |
7758 | |
7759 if (cin_nocode(l)) | |
7760 continue; | |
7761 | |
7762 terminated = cin_isterminated(l, FALSE, TRUE); | |
7763 | |
7764 /* | |
7765 * If we are at top level and the line looks like a | |
7766 * function declaration, we are done | |
7767 * (it's a variable declaration). | |
7768 */ | |
7769 if (start_brace != BRACE_IN_COL0 | |
5442 | 7770 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
7 | 7771 { |
7772 /* if the line is terminated with another ',' | |
7773 * it is a continued variable initialization. | |
7774 * don't add extra indent. | |
7775 * TODO: does not work, if a function | |
7776 * declaration is split over multiple lines: | |
7777 * cin_isfuncdecl returns FALSE then. | |
7778 */ | |
7779 if (terminated == ',') | |
7780 break; | |
7781 | |
7782 /* if it es a enum declaration or an assignment, | |
7783 * we are done. | |
7784 */ | |
7785 if (terminated != ';' && cin_isinit()) | |
7786 break; | |
7787 | |
7788 /* nothing useful found */ | |
7789 if (terminated == 0 || terminated == '{') | |
7790 continue; | |
7791 } | |
7792 | |
7793 if (terminated != ';') | |
7794 { | |
7795 /* Skip parens and braces. Position the cursor | |
7796 * over the rightmost paren, so that matching it | |
7797 * will take us back to the start of the line. | |
7798 */ /* XXX */ | |
7799 trypos = NULL; | |
7800 if (find_last_paren(l, '(', ')')) | |
5438 | 7801 trypos = find_match_paren( |
5442 | 7802 curbuf->b_ind_maxparen); |
7 | 7803 |
7804 if (trypos == NULL && find_last_paren(l, '{', '}')) | |
5442 | 7805 trypos = find_start_brace(); |
7 | 7806 |
7807 if (trypos != NULL) | |
7808 { | |
7809 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7810 curwin->w_cursor.col = 0; |
7 | 7811 continue; |
7812 } | |
7813 } | |
7814 | |
7815 /* it's a variable declaration, add indentation | |
7816 * like in | |
7817 * int a, | |
7818 * b; | |
7819 */ | |
7820 if (cont_amount > 0) | |
7821 amount = cont_amount; | |
7822 else | |
7823 amount += ind_continuation; | |
7824 } | |
7825 else if (lookfor == LOOKFOR_UNTERM) | |
7826 { | |
7827 if (cont_amount > 0) | |
7828 amount = cont_amount; | |
7829 else | |
7830 amount += ind_continuation; | |
7831 } | |
3123 | 7832 else |
2857 | 7833 { |
3123 | 7834 if (lookfor != LOOKFOR_TERM |
7835 && lookfor != LOOKFOR_CPP_BASECLASS) | |
2857 | 7836 { |
3123 | 7837 amount = scope_amount; |
7838 if (theline[0] == '{') | |
7839 { | |
5438 | 7840 amount += curbuf->b_ind_open_extra; |
7841 added_to_amount = curbuf->b_ind_open_extra; | |
3123 | 7842 } |
2857 | 7843 } |
7844 | |
3123 | 7845 if (lookfor_cpp_namespace) |
2857 | 7846 { |
3123 | 7847 /* |
7848 * Looking for C++ namespace, need to look further | |
7849 * back. | |
7850 */ | |
7851 if (curwin->w_cursor.lnum == ourscope) | |
7852 continue; | |
7853 | |
7854 if (curwin->w_cursor.lnum == 0 | |
7855 || curwin->w_cursor.lnum | |
7856 < ourscope - FIND_NAMESPACE_LIM) | |
7857 break; | |
7858 | |
7859 l = ml_get_curline(); | |
7860 | |
7861 /* If we're in a comment now, skip to the start of | |
7862 * the comment. */ | |
5442 | 7863 trypos = ind_find_start_comment(); |
3123 | 7864 if (trypos != NULL) |
7865 { | |
7866 curwin->w_cursor.lnum = trypos->lnum + 1; | |
7867 curwin->w_cursor.col = 0; | |
7868 continue; | |
7869 } | |
7870 | |
7871 /* Skip preprocessor directives and blank lines. */ | |
7872 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) | |
7873 continue; | |
7874 | |
7875 /* Finally the actual check for "namespace". */ | |
7876 if (cin_is_cpp_namespace(l)) | |
7877 { | |
5438 | 7878 amount += curbuf->b_ind_cpp_namespace |
7879 - added_to_amount; | |
3123 | 7880 break; |
7881 } | |
7882 | |
7883 if (cin_nocode(l)) | |
7884 continue; | |
2857 | 7885 } |
7 | 7886 } |
7887 break; | |
7888 } | |
7889 | |
7890 /* | |
7891 * If we're in a comment now, skip to the start of the comment. | |
7892 */ /* XXX */ | |
5442 | 7893 if ((trypos = ind_find_start_comment()) != NULL) |
7 | 7894 { |
7895 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7896 curwin->w_cursor.col = 0; |
7 | 7897 continue; |
7898 } | |
7899 | |
7900 l = ml_get_curline(); | |
7901 | |
7902 /* | |
7903 * If this is a switch() label, may line up relative to that. | |
827 | 7904 * If this is a C++ scope declaration, do the same. |
7 | 7905 */ |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7906 iscase = cin_iscase(l, FALSE); |
7 | 7907 if (iscase || cin_isscopedecl(l)) |
7908 { | |
7909 /* we are only looking for cpp base class | |
7910 * declaration/initialization any longer */ | |
7911 if (lookfor == LOOKFOR_CPP_BASECLASS) | |
7912 break; | |
7913 | |
7914 /* When looking for a "do" we are not interested in | |
7915 * labels. */ | |
7916 if (whilelevel > 0) | |
7917 continue; | |
7918 | |
7919 /* | |
7920 * case xx: | |
7921 * c = 99 + <- this indent plus continuation | |
7922 *-> here; | |
7923 */ | |
7924 if (lookfor == LOOKFOR_UNTERM | |
7925 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
7926 { | |
7927 if (cont_amount > 0) | |
7928 amount = cont_amount; | |
7929 else | |
7930 amount += ind_continuation; | |
7931 break; | |
7932 } | |
7933 | |
7934 /* | |
7935 * case xx: <- line up with this case | |
7936 * x = 333; | |
7937 * case yy: | |
7938 */ | |
7939 if ( (iscase && lookfor == LOOKFOR_CASE) | |
7940 || (iscase && lookfor_break) | |
7941 || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) | |
7942 { | |
7943 /* | |
7944 * Check that this case label is not for another | |
7945 * switch() | |
7946 */ /* XXX */ | |
5442 | 7947 if ((trypos = find_start_brace()) == NULL |
5438 | 7948 || trypos->lnum == ourscope) |
7 | 7949 { |
7950 amount = get_indent(); /* XXX */ | |
7951 break; | |
7952 } | |
7953 continue; | |
7954 } | |
7955 | |
7956 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ | |
7957 | |
7958 /* | |
7959 * case xx: if (cond) <- line up with this if | |
7960 * y = y + 1; | |
7961 * -> s = 99; | |
7962 * | |
7963 * case xx: | |
7964 * if (cond) <- line up with this line | |
7965 * y = y + 1; | |
7966 * -> s = 99; | |
7967 */ | |
7968 if (lookfor == LOOKFOR_TERM) | |
7969 { | |
7970 if (n) | |
7971 amount = n; | |
7972 | |
7973 if (!lookfor_break) | |
7974 break; | |
7975 } | |
7976 | |
7977 /* | |
7978 * case xx: x = x + 1; <- line up with this x | |
7979 * -> y = y + 1; | |
7980 * | |
7981 * case xx: if (cond) <- line up with this if | |
7982 * -> y = y + 1; | |
7983 */ | |
7984 if (n) | |
7985 { | |
7986 amount = n; | |
7987 l = after_label(ml_get_curline()); | |
7988 if (l != NULL && cin_is_cinword(l)) | |
829 | 7989 { |
7990 if (theline[0] == '{') | |
5438 | 7991 amount += curbuf->b_ind_open_extra; |
829 | 7992 else |
5438 | 7993 amount += curbuf->b_ind_level |
7994 + curbuf->b_ind_no_brace; | |
829 | 7995 } |
7 | 7996 break; |
7997 } | |
7998 | |
7999 /* | |
8000 * Try to get the indent of a statement before the switch | |
8001 * label. If nothing is found, line up relative to the | |
8002 * switch label. | |
8003 * break; <- may line up with this line | |
8004 * case xx: | |
8005 * -> y = 1; | |
8006 */ | |
8007 scope_amount = get_indent() + (iscase /* XXX */ | |
5438 | 8008 ? curbuf->b_ind_case_code |
8009 : curbuf->b_ind_scopedecl_code); | |
8010 lookfor = curbuf->b_ind_case_break | |
8011 ? LOOKFOR_NOBREAK : LOOKFOR_ANY; | |
7 | 8012 continue; |
8013 } | |
8014 | |
8015 /* | |
8016 * Looking for a switch() label or C++ scope declaration, | |
8017 * ignore other lines, skip {}-blocks. | |
8018 */ | |
8019 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) | |
8020 { | |
5442 | 8021 if (find_last_paren(l, '{', '}') |
8022 && (trypos = find_start_brace()) != NULL) | |
1550 | 8023 { |
7 | 8024 curwin->w_cursor.lnum = trypos->lnum + 1; |
1550 | 8025 curwin->w_cursor.col = 0; |
8026 } | |
7 | 8027 continue; |
8028 } | |
8029 | |
8030 /* | |
8031 * Ignore jump labels with nothing after them. | |
8032 */ | |
5442 | 8033 if (!curbuf->b_ind_js && cin_islabel()) |
7 | 8034 { |
8035 l = after_label(ml_get_curline()); | |
8036 if (l == NULL || cin_nocode(l)) | |
8037 continue; | |
8038 } | |
8039 | |
8040 /* | |
8041 * Ignore #defines, #if, etc. | |
8042 * Ignore comment and empty lines. | |
8043 * (need to get the line again, cin_islabel() may have | |
8044 * unlocked it) | |
8045 */ | |
8046 l = ml_get_curline(); | |
8047 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum) | |
8048 || cin_nocode(l)) | |
8049 continue; | |
8050 | |
8051 /* | |
8052 * Are we at the start of a cpp base class declaration or | |
8053 * constructor initialization? | |
8054 */ /* XXX */ | |
827 | 8055 n = FALSE; |
5438 | 8056 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0) |
827 | 8057 { |
1336 | 8058 n = cin_is_cpp_baseclass(&col); |
827 | 8059 l = ml_get_curline(); |
8060 } | |
8061 if (n) | |
7 | 8062 { |
8063 if (lookfor == LOOKFOR_UNTERM) | |
8064 { | |
8065 if (cont_amount > 0) | |
8066 amount = cont_amount; | |
8067 else | |
8068 amount += ind_continuation; | |
8069 } | |
828 | 8070 else if (theline[0] == '{') |
7 | 8071 { |
828 | 8072 /* Need to find start of the declaration. */ |
8073 lookfor = LOOKFOR_UNTERM; | |
8074 ind_continuation = 0; | |
8075 continue; | |
7 | 8076 } |
8077 else | |
828 | 8078 /* XXX */ |
5442 | 8079 amount = get_baseclass_amount(col); |
7 | 8080 break; |
8081 } | |
8082 else if (lookfor == LOOKFOR_CPP_BASECLASS) | |
8083 { | |
8084 /* only look, whether there is a cpp base class | |
827 | 8085 * declaration or initialization before the opening brace. |
8086 */ | |
7 | 8087 if (cin_isterminated(l, TRUE, FALSE)) |
8088 break; | |
8089 else | |
8090 continue; | |
8091 } | |
8092 | |
8093 /* | |
8094 * What happens next depends on the line being terminated. | |
8095 * If terminated with a ',' only consider it terminating if | |
1224 | 8096 * there is another unterminated statement behind, eg: |
7 | 8097 * 123, |
8098 * sizeof | |
8099 * here | |
8100 * Otherwise check whether it is a enumeration or structure | |
8101 * initialisation (not indented) or a variable declaration | |
8102 * (indented). | |
8103 */ | |
8104 terminated = cin_isterminated(l, FALSE, TRUE); | |
8105 | |
6030 | 8106 if (js_cur_has_key) |
8107 { | |
8108 js_cur_has_key = 0; /* only check the first line */ | |
8109 if (curbuf->b_ind_js && terminated == ',') | |
8110 { | |
8111 /* For Javascript we might be inside an object: | |
8112 * key: something, <- align with this | |
8113 * key: something | |
8114 * or: | |
8115 * key: something + <- align with this | |
8116 * something, | |
8117 * key: something | |
8118 */ | |
8119 lookfor = LOOKFOR_JS_KEY; | |
8120 } | |
8121 } | |
8122 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l)) | |
8123 { | |
8124 amount = get_indent(); | |
8125 break; | |
8126 } | |
8127 if (lookfor == LOOKFOR_NO_COMMA) | |
8128 { | |
8129 if (terminated != ',') | |
8130 /* line below current line is the one that starts a | |
8131 * (possibly broken) line ending in a comma. */ | |
8132 break; | |
8133 amount = get_indent(); | |
8134 if (curwin->w_cursor.lnum - 1 == ourscope) | |
8135 /* line above is start of the scope, thus current line | |
8136 * is the one that stars a (possibly broken) line | |
8137 * ending in a comma. */ | |
8138 break; | |
8139 } | |
8140 | |
7 | 8141 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM |
8142 && terminated == ',')) | |
8143 { | |
8144 /* | |
8145 * if we're in the middle of a paren thing, | |
8146 * go back to the line that starts it so | |
8147 * we can get the right prevailing indent | |
8148 * if ( foo && | |
8149 * bar ) | |
8150 */ | |
8151 /* | |
6030 | 8152 * Position the cursor over the rightmost paren, so that |
7 | 8153 * matching it will take us back to the start of the line. |
6030 | 8154 * Ignore a match before the start of the block. |
7 | 8155 */ |
8156 (void)find_last_paren(l, '(', ')'); | |
5442 | 8157 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos)); |
6030 | 8158 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum |
8159 || (trypos->lnum == tryposBrace->lnum | |
8160 && trypos->col < tryposBrace->col))) | |
8161 trypos = NULL; | |
7 | 8162 |
8163 /* | |
8164 * If we are looking for ',', we also look for matching | |
8165 * braces. | |
8166 */ | |
828 | 8167 if (trypos == NULL && terminated == ',' |
8168 && find_last_paren(l, '{', '}')) | |
5442 | 8169 trypos = find_start_brace(); |
7 | 8170 |
8171 if (trypos != NULL) | |
8172 { | |
8173 /* | |
8174 * Check if we are on a case label now. This is | |
8175 * handled above. | |
8176 * case xx: if ( asdf && | |
8177 * asdf) | |
8178 */ | |
1550 | 8179 curwin->w_cursor = *trypos; |
7 | 8180 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
|
8181 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
7 | 8182 { |
8183 ++curwin->w_cursor.lnum; | |
1550 | 8184 curwin->w_cursor.col = 0; |
7 | 8185 continue; |
8186 } | |
8187 } | |
8188 | |
8189 /* | |
8190 * Skip over continuation lines to find the one to get the | |
8191 * indent from | |
8192 * char *usethis = "bla\ | |
8193 * bla", | |
8194 * here; | |
8195 */ | |
8196 if (terminated == ',') | |
8197 { | |
8198 while (curwin->w_cursor.lnum > 1) | |
8199 { | |
8200 l = ml_get(curwin->w_cursor.lnum - 1); | |
8201 if (*l == NUL || l[STRLEN(l) - 1] != '\\') | |
8202 break; | |
8203 --curwin->w_cursor.lnum; | |
1550 | 8204 curwin->w_cursor.col = 0; |
7 | 8205 } |
8206 } | |
8207 | |
8208 /* | |
8209 * Get indent and pointer to text for current line, | |
8210 * ignoring any jump label. XXX | |
8211 */ | |
6030 | 8212 if (curbuf->b_ind_js) |
8213 cur_amount = get_indent(); | |
8214 else | |
5442 | 8215 cur_amount = skip_label(curwin->w_cursor.lnum, &l); |
7 | 8216 /* |
8217 * If this is just above the line we are indenting, and it | |
8218 * starts with a '{', line it up with this line. | |
8219 * while (not) | |
8220 * -> { | |
8221 * } | |
8222 */ | |
8223 if (terminated != ',' && lookfor != LOOKFOR_TERM | |
8224 && theline[0] == '{') | |
8225 { | |
8226 amount = cur_amount; | |
8227 /* | |
5438 | 8228 * Only add b_ind_open_extra when the current line |
7 | 8229 * doesn't start with a '{', which must have a match |
8230 * in the same line (scope is the same). Probably: | |
8231 * { 1, 2 }, | |
8232 * -> { 3, 4 } | |
8233 */ | |
8234 if (*skipwhite(l) != '{') | |
5438 | 8235 amount += curbuf->b_ind_open_extra; |
8236 | |
6030 | 8237 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) |
7 | 8238 { |
8239 /* have to look back, whether it is a cpp base | |
8240 * class declaration or initialization */ | |
8241 lookfor = LOOKFOR_CPP_BASECLASS; | |
8242 continue; | |
8243 } | |
8244 break; | |
8245 } | |
8246 | |
8247 /* | |
8248 * Check if we are after an "if", "while", etc. | |
8249 * Also allow " } else". | |
8250 */ | |
8251 if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) | |
8252 { | |
8253 /* | |
8254 * Found an unterminated line after an if (), line up | |
8255 * with the last one. | |
8256 * if (cond) | |
8257 * 100 + | |
8258 * -> here; | |
8259 */ | |
8260 if (lookfor == LOOKFOR_UNTERM | |
8261 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
8262 { | |
8263 if (cont_amount > 0) | |
8264 amount = cont_amount; | |
8265 else | |
8266 amount += ind_continuation; | |
8267 break; | |
8268 } | |
8269 | |
8270 /* | |
8271 * If this is just above the line we are indenting, we | |
8272 * are finished. | |
8273 * while (not) | |
8274 * -> here; | |
8275 * Otherwise this indent can be used when the line | |
8276 * before this is terminated. | |
8277 * yyy; | |
8278 * if (stat) | |
8279 * while (not) | |
8280 * xxx; | |
8281 * -> here; | |
8282 */ | |
8283 amount = cur_amount; | |
8284 if (theline[0] == '{') | |
5438 | 8285 amount += curbuf->b_ind_open_extra; |
7 | 8286 if (lookfor != LOOKFOR_TERM) |
8287 { | |
5438 | 8288 amount += curbuf->b_ind_level |
8289 + curbuf->b_ind_no_brace; | |
7 | 8290 break; |
8291 } | |
8292 | |
8293 /* | |
8294 * Special trick: when expecting the while () after a | |
8295 * do, line up with the while() | |
8296 * do | |
8297 * x = 1; | |
8298 * -> here | |
8299 */ | |
8300 l = skipwhite(ml_get_curline()); | |
8301 if (cin_isdo(l)) | |
8302 { | |
8303 if (whilelevel == 0) | |
8304 break; | |
8305 --whilelevel; | |
8306 } | |
8307 | |
8308 /* | |
8309 * When searching for a terminated line, don't use the | |
2855 | 8310 * one between the "if" and the matching "else". |
7 | 8311 * Need to use the scope of this "else". XXX |
8312 * If whilelevel != 0 continue looking for a "do {". | |
8313 */ | |
2855 | 8314 if (cin_iselse(l) && whilelevel == 0) |
8315 { | |
8316 /* If we're looking at "} else", let's make sure we | |
8317 * find the opening brace of the enclosing scope, | |
8318 * not the one from "if () {". */ | |
8319 if (*l == '}') | |
8320 curwin->w_cursor.col = | |
2861 | 8321 (colnr_T)(l - ml_get_curline()) + 1; |
2855 | 8322 |
5442 | 8323 if ((trypos = find_start_brace()) == NULL |
8324 || find_match(LOOKFOR_IF, trypos->lnum) | |
8325 == FAIL) | |
2855 | 8326 break; |
8327 } | |
7 | 8328 } |
8329 | |
8330 /* | |
8331 * If we're below an unterminated line that is not an | |
8332 * "if" or something, we may line up with this line or | |
1224 | 8333 * add something for a continuation line, depending on |
7 | 8334 * the line before this one. |
8335 */ | |
8336 else | |
8337 { | |
8338 /* | |
8339 * Found two unterminated lines on a row, line up with | |
8340 * the last one. | |
8341 * c = 99 + | |
8342 * 100 + | |
8343 * -> here; | |
8344 */ | |
8345 if (lookfor == LOOKFOR_UNTERM) | |
8346 { | |
8347 /* When line ends in a comma add extra indent */ | |
8348 if (terminated == ',') | |
8349 amount += ind_continuation; | |
8350 break; | |
8351 } | |
8352 | |
8353 if (lookfor == LOOKFOR_ENUM_OR_INIT) | |
8354 { | |
8355 /* Found two lines ending in ',', lineup with the | |
8356 * lowest one, but check for cpp base class | |
8357 * declaration/initialization, if it is an | |
8358 * opening brace or we are looking just for | |
8359 * enumerations/initializations. */ | |
8360 if (terminated == ',') | |
8361 { | |
5438 | 8362 if (curbuf->b_ind_cpp_baseclass == 0) |
7 | 8363 break; |
8364 | |
8365 lookfor = LOOKFOR_CPP_BASECLASS; | |
8366 continue; | |
8367 } | |
8368 | |
8369 /* Ignore unterminated lines in between, but | |
8370 * reduce indent. */ | |
8371 if (amount > cur_amount) | |
8372 amount = cur_amount; | |
8373 } | |
8374 else | |
8375 { | |
8376 /* | |
8377 * Found first unterminated line on a row, may | |
8378 * line up with this line, remember its indent | |
8379 * 100 + | |
8380 * -> here; | |
8381 */ | |
8382 amount = cur_amount; | |
8383 | |
8384 /* | |
8385 * If previous line ends in ',', check whether we | |
8386 * are in an initialization or enum | |
8387 * struct xxx = | |
8388 * { | |
8389 * sizeof a, | |
8390 * 124 }; | |
8391 * or a normal possible continuation line. | |
8392 * but only, of no other statement has been found | |
8393 * yet. | |
8394 */ | |
8395 if (lookfor == LOOKFOR_INITIAL && terminated == ',') | |
8396 { | |
6030 | 8397 if (curbuf->b_ind_js) |
8398 { | |
8399 /* Search for a line ending in a comma | |
8400 * and line up with the line below it | |
8401 * (could be the current line). | |
8402 * some = [ | |
8403 * 1, <- line up here | |
8404 * 2, | |
8405 * some = [ | |
8406 * 3 + <- line up here | |
8407 * 4 * | |
8408 * 5, | |
8409 * 6, | |
8410 */ | |
8411 lookfor = LOOKFOR_NO_COMMA; | |
8412 amount = get_indent(); /* XXX */ | |
8413 trypos = find_match_char('[', | |
8414 curbuf->b_ind_maxparen); | |
8415 if (trypos != NULL) | |
8416 { | |
8417 if (trypos->lnum | |
8418 == curwin->w_cursor.lnum - 1) | |
8419 { | |
8420 /* Current line is first inside | |
8421 * [], line up with it. */ | |
8422 break; | |
8423 } | |
8424 ourscope = trypos->lnum; | |
8425 } | |
8426 } | |
8427 else | |
8428 { | |
8429 lookfor = LOOKFOR_ENUM_OR_INIT; | |
8430 cont_amount = cin_first_id_amount(); | |
8431 } | |
7 | 8432 } |
8433 else | |
8434 { | |
8435 if (lookfor == LOOKFOR_INITIAL | |
8436 && *l != NUL | |
8437 && l[STRLEN(l) - 1] == '\\') | |
8438 /* XXX */ | |
8439 cont_amount = cin_get_equal_amount( | |
8440 curwin->w_cursor.lnum); | |
6030 | 8441 if (lookfor != LOOKFOR_TERM |
8442 && lookfor != LOOKFOR_JS_KEY) | |
7 | 8443 lookfor = LOOKFOR_UNTERM; |
8444 } | |
8445 } | |
8446 } | |
8447 } | |
8448 | |
8449 /* | |
8450 * Check if we are after a while (cond); | |
8451 * If so: Ignore until the matching "do". | |
8452 */ | |
6030 | 8453 else if (cin_iswhileofdo_end(terminated)) /* XXX */ |
7 | 8454 { |
8455 /* | |
8456 * Found an unterminated line after a while ();, line up | |
8457 * with the last one. | |
8458 * while (cond); | |
8459 * 100 + <- line up with this one | |
8460 * -> here; | |
8461 */ | |
8462 if (lookfor == LOOKFOR_UNTERM | |
8463 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
8464 { | |
8465 if (cont_amount > 0) | |
8466 amount = cont_amount; | |
8467 else | |
8468 amount += ind_continuation; | |
8469 break; | |
8470 } | |
8471 | |
8472 if (whilelevel == 0) | |
8473 { | |
8474 lookfor = LOOKFOR_TERM; | |
8475 amount = get_indent(); /* XXX */ | |
8476 if (theline[0] == '{') | |
5438 | 8477 amount += curbuf->b_ind_open_extra; |
7 | 8478 } |
8479 ++whilelevel; | |
8480 } | |
8481 | |
8482 /* | |
8483 * We are after a "normal" statement. | |
8484 * If we had another statement we can stop now and use the | |
8485 * indent of that other statement. | |
8486 * Otherwise the indent of the current statement may be used, | |
8487 * search backwards for the next "normal" statement. | |
8488 */ | |
8489 else | |
8490 { | |
8491 /* | |
8492 * Skip single break line, if before a switch label. It | |
8493 * may be lined up with the case label. | |
8494 */ | |
8495 if (lookfor == LOOKFOR_NOBREAK | |
8496 && cin_isbreak(skipwhite(ml_get_curline()))) | |
8497 { | |
8498 lookfor = LOOKFOR_ANY; | |
8499 continue; | |
8500 } | |
8501 | |
8502 /* | |
8503 * Handle "do {" line. | |
8504 */ | |
8505 if (whilelevel > 0) | |
8506 { | |
8507 l = cin_skipcomment(ml_get_curline()); | |
8508 if (cin_isdo(l)) | |
8509 { | |
8510 amount = get_indent(); /* XXX */ | |
8511 --whilelevel; | |
8512 continue; | |
8513 } | |
8514 } | |
8515 | |
8516 /* | |
8517 * Found a terminated line above an unterminated line. Add | |
8518 * the amount for a continuation line. | |
8519 * x = 1; | |
8520 * y = foo + | |
8521 * -> here; | |
8522 * or | |
8523 * int x = 1; | |
8524 * int foo, | |
8525 * -> here; | |
8526 */ | |
8527 if (lookfor == LOOKFOR_UNTERM | |
8528 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
8529 { | |
8530 if (cont_amount > 0) | |
8531 amount = cont_amount; | |
8532 else | |
8533 amount += ind_continuation; | |
8534 break; | |
8535 } | |
8536 | |
8537 /* | |
8538 * Found a terminated line above a terminated line or "if" | |
8539 * etc. line. Use the amount of the line below us. | |
8540 * x = 1; x = 1; | |
8541 * if (asdf) y = 2; | |
8542 * while (asdf) ->here; | |
8543 * here; | |
8544 * ->foo; | |
8545 */ | |
8546 if (lookfor == LOOKFOR_TERM) | |
8547 { | |
8548 if (!lookfor_break && whilelevel == 0) | |
8549 break; | |
8550 } | |
8551 | |
8552 /* | |
8553 * First line above the one we're indenting is terminated. | |
8554 * To know what needs to be done look further backward for | |
8555 * a terminated line. | |
8556 */ | |
8557 else | |
8558 { | |
8559 /* | |
8560 * position the cursor over the rightmost paren, so | |
8561 * that matching it will take us back to the start of | |
8562 * the line. Helps for: | |
8563 * func(asdr, | |
8564 * asdfasdf); | |
8565 * here; | |
8566 */ | |
8567 term_again: | |
8568 l = ml_get_curline(); | |
8569 if (find_last_paren(l, '(', ')') | |
5438 | 8570 && (trypos = find_match_paren( |
5442 | 8571 curbuf->b_ind_maxparen)) != NULL) |
7 | 8572 { |
8573 /* | |
8574 * Check if we are on a case label now. This is | |
8575 * handled above. | |
8576 * case xx: if ( asdf && | |
8577 * asdf) | |
8578 */ | |
1550 | 8579 curwin->w_cursor = *trypos; |
7 | 8580 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
|
8581 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
7 | 8582 { |
8583 ++curwin->w_cursor.lnum; | |
1550 | 8584 curwin->w_cursor.col = 0; |
7 | 8585 continue; |
8586 } | |
8587 } | |
8588 | |
8589 /* When aligning with the case statement, don't align | |
8590 * with a statement after it. | |
8591 * case 1: { <-- don't use this { position | |
8592 * stat; | |
8593 * } | |
8594 * case 2: | |
8595 * stat; | |
8596 * } | |
8597 */ | |
5438 | 8598 iscase = (curbuf->b_ind_keep_case_label |
8599 && cin_iscase(l, FALSE)); | |
7 | 8600 |
8601 /* | |
8602 * Get indent and pointer to text for current line, | |
8603 * ignoring any jump label. | |
8604 */ | |
5442 | 8605 amount = skip_label(curwin->w_cursor.lnum, &l); |
7 | 8606 |
8607 if (theline[0] == '{') | |
5438 | 8608 amount += curbuf->b_ind_open_extra; |
8609 /* See remark above: "Only add b_ind_open_extra.." */ | |
827 | 8610 l = skipwhite(l); |
8611 if (*l == '{') | |
5438 | 8612 amount -= curbuf->b_ind_open_extra; |
7 | 8613 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; |
8614 | |
8615 /* | |
827 | 8616 * When a terminated line starts with "else" skip to |
8617 * the matching "if": | |
8618 * else 3; | |
856 | 8619 * indent this; |
827 | 8620 * Need to use the scope of this "else". XXX |
8621 * If whilelevel != 0 continue looking for a "do {". | |
8622 */ | |
8623 if (lookfor == LOOKFOR_TERM | |
8624 && *l != '}' | |
8625 && cin_iselse(l) | |
8626 && whilelevel == 0) | |
8627 { | |
5442 | 8628 if ((trypos = find_start_brace()) == NULL |
8629 || find_match(LOOKFOR_IF, trypos->lnum) | |
8630 == FAIL) | |
827 | 8631 break; |
8632 continue; | |
8633 } | |
8634 | |
8635 /* | |
7 | 8636 * If we're at the end of a block, skip to the start of |
8637 * that block. | |
8638 */ | |
3172 | 8639 l = ml_get_curline(); |
5442 | 8640 if (find_last_paren(l, '{', '}') /* XXX */ |
8641 && (trypos = find_start_brace()) != NULL) | |
7 | 8642 { |
1550 | 8643 curwin->w_cursor = *trypos; |
7 | 8644 /* if not "else {" check for terminated again */ |
8645 /* but skip block for "} else {" */ | |
8646 l = cin_skipcomment(ml_get_curline()); | |
8647 if (*l == '}' || !cin_iselse(l)) | |
8648 goto term_again; | |
8649 ++curwin->w_cursor.lnum; | |
1550 | 8650 curwin->w_cursor.col = 0; |
7 | 8651 } |
8652 } | |
8653 } | |
8654 } | |
8655 } | |
8656 } | |
8657 | |
8658 /* add extra indent for a comment */ | |
8659 if (cin_iscomment(theline)) | |
5438 | 8660 amount += curbuf->b_ind_comment; |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
8661 |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
8662 /* subtract extra left-shift for jump labels */ |
5438 | 8663 if (curbuf->b_ind_jump_label > 0 && original_line_islabel) |
8664 amount -= curbuf->b_ind_jump_label; | |
7 | 8665 } |
8666 else | |
8667 { | |
8668 /* | |
6030 | 8669 * ok -- we're not inside any sort of structure at all! |
8670 * | |
8671 * This means we're at the top level, and everything should | |
8672 * basically just match where the previous line is, except | |
8673 * for the lines immediately following a function declaration, | |
8674 * which are K&R-style parameters and need to be indented. | |
8675 * | |
7 | 8676 * if our line starts with an open brace, forget about any |
8677 * prevailing indent and make sure it looks like the start | |
8678 * of a function | |
8679 */ | |
8680 | |
8681 if (theline[0] == '{') | |
8682 { | |
5438 | 8683 amount = curbuf->b_ind_first_open; |
7 | 8684 } |
8685 | |
8686 /* | |
8687 * If the NEXT line is a function declaration, the current | |
8688 * 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
|
8689 * 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
|
8690 * 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
|
8691 * contains { or }: "void f() {\n if (1)" |
7 | 8692 */ |
8693 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count | |
8694 && !cin_nocode(theline) | |
2094
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
8695 && vim_strchr(theline, '{') == NULL |
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
8696 && vim_strchr(theline, '}') == NULL |
7 | 8697 && !cin_ends_in(theline, (char_u *)":", NULL) |
8698 && !cin_ends_in(theline, (char_u *)",", NULL) | |
3220 | 8699 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, |
5442 | 8700 cur_curpos.lnum + 1) |
7 | 8701 && !cin_isterminated(theline, FALSE, TRUE)) |
8702 { | |
5438 | 8703 amount = curbuf->b_ind_func_type; |
7 | 8704 } |
8705 else | |
8706 { | |
8707 amount = 0; | |
8708 curwin->w_cursor = cur_curpos; | |
8709 | |
8710 /* search backwards until we find something we recognize */ | |
8711 | |
8712 while (curwin->w_cursor.lnum > 1) | |
8713 { | |
8714 curwin->w_cursor.lnum--; | |
8715 curwin->w_cursor.col = 0; | |
8716 | |
8717 l = ml_get_curline(); | |
8718 | |
8719 /* | |
8720 * If we're in a comment now, skip to the start of the comment. | |
8721 */ /* XXX */ | |
5442 | 8722 if ((trypos = ind_find_start_comment()) != NULL) |
7 | 8723 { |
8724 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 8725 curwin->w_cursor.col = 0; |
7 | 8726 continue; |
8727 } | |
8728 | |
8729 /* | |
827 | 8730 * Are we at the start of a cpp base class declaration or |
8731 * constructor initialization? | |
7 | 8732 */ /* XXX */ |
827 | 8733 n = FALSE; |
5438 | 8734 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{') |
827 | 8735 { |
1336 | 8736 n = cin_is_cpp_baseclass(&col); |
827 | 8737 l = ml_get_curline(); |
8738 } | |
8739 if (n) | |
7 | 8740 { |
828 | 8741 /* XXX */ |
5442 | 8742 amount = get_baseclass_amount(col); |
7 | 8743 break; |
8744 } | |
8745 | |
8746 /* | |
8747 * Skip preprocessor directives and blank lines. | |
8748 */ | |
8749 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) | |
8750 continue; | |
8751 | |
8752 if (cin_nocode(l)) | |
8753 continue; | |
8754 | |
8755 /* | |
8756 * If the previous line ends in ',', use one level of | |
8757 * indentation: | |
8758 * int foo, | |
8759 * bar; | |
8760 * do this before checking for '}' in case of eg. | |
8761 * enum foobar | |
8762 * { | |
8763 * ... | |
8764 * } foo, | |
8765 * bar; | |
8766 */ | |
8767 n = 0; | |
8768 if (cin_ends_in(l, (char_u *)",", NULL) | |
8769 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) | |
8770 { | |
8771 /* take us back to opening paren */ | |
8772 if (find_last_paren(l, '(', ')') | |
5438 | 8773 && (trypos = find_match_paren( |
5442 | 8774 curbuf->b_ind_maxparen)) != NULL) |
1550 | 8775 curwin->w_cursor = *trypos; |
7 | 8776 |
8777 /* For a line ending in ',' that is a continuation line go | |
8778 * back to the first line with a backslash: | |
8779 * char *foo = "bla\ | |
8780 * bla", | |
8781 * here; | |
8782 */ | |
8783 while (n == 0 && curwin->w_cursor.lnum > 1) | |
8784 { | |
8785 l = ml_get(curwin->w_cursor.lnum - 1); | |
8786 if (*l == NUL || l[STRLEN(l) - 1] != '\\') | |
8787 break; | |
8788 --curwin->w_cursor.lnum; | |
1550 | 8789 curwin->w_cursor.col = 0; |
7 | 8790 } |
8791 | |
8792 amount = get_indent(); /* XXX */ | |
8793 | |
8794 if (amount == 0) | |
8795 amount = cin_first_id_amount(); | |
8796 if (amount == 0) | |
8797 amount = ind_continuation; | |
8798 break; | |
8799 } | |
8800 | |
8801 /* | |
8802 * If the line looks like a function declaration, and we're | |
8803 * not in a comment, put it the left margin. | |
8804 */ | |
5442 | 8805 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */ |
7 | 8806 break; |
8807 l = ml_get_curline(); | |
8808 | |
8809 /* | |
8810 * Finding the closing '}' of a previous function. Put | |
8811 * current line at the left margin. For when 'cino' has "fs". | |
8812 */ | |
8813 if (*skipwhite(l) == '}') | |
8814 break; | |
8815 | |
8816 /* (matching {) | |
8817 * If the previous line ends on '};' (maybe followed by | |
8818 * comments) align at column 0. For example: | |
8819 * char *string_array[] = { "foo", | |
8820 * / * x * / "b};ar" }; / * foobar * / | |
8821 */ | |
8822 if (cin_ends_in(l, (char_u *)"};", NULL)) | |
8823 break; | |
8824 | |
8825 /* | |
6030 | 8826 * If the previous line ends on '[' we are probably in an |
8827 * array constant: | |
8828 * something = [ | |
8829 * 234, <- extra indent | |
8830 */ | |
8831 if (cin_ends_in(l, (char_u *)"[", NULL)) | |
8832 { | |
8833 amount = get_indent() + ind_continuation; | |
8834 break; | |
8835 } | |
8836 | |
8837 /* | |
3186 | 8838 * Find a line only has a semicolon that belongs to a previous |
8839 * line ending in '}', e.g. before an #endif. Don't increase | |
8840 * indent then. | |
8841 */ | |
8842 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) | |
8843 { | |
8844 pos_T curpos_save = curwin->w_cursor; | |
8845 | |
8846 while (curwin->w_cursor.lnum > 1) | |
8847 { | |
8848 look = ml_get(--curwin->w_cursor.lnum); | |
8849 if (!(cin_nocode(look) || cin_ispreproc_cont( | |
8850 &look, &curwin->w_cursor.lnum))) | |
8851 break; | |
8852 } | |
8853 if (curwin->w_cursor.lnum > 0 | |
8854 && cin_ends_in(look, (char_u *)"}", NULL)) | |
8855 break; | |
8856 | |
8857 curwin->w_cursor = curpos_save; | |
8858 } | |
8859 | |
8860 /* | |
7 | 8861 * If the PREVIOUS line is a function declaration, the current |
8862 * line (and the ones that follow) needs to be indented as | |
8863 * parameters. | |
8864 */ | |
5442 | 8865 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
7 | 8866 { |
5438 | 8867 amount = curbuf->b_ind_param; |
7 | 8868 break; |
8869 } | |
8870 | |
8871 /* | |
8872 * If the previous line ends in ';' and the line before the | |
8873 * previous line ends in ',' or '\', ident to column zero: | |
8874 * int foo, | |
8875 * bar; | |
8876 * indent_to_0 here; | |
8877 */ | |
828 | 8878 if (cin_ends_in(l, (char_u *)";", NULL)) |
7 | 8879 { |
8880 l = ml_get(curwin->w_cursor.lnum - 1); | |
8881 if (cin_ends_in(l, (char_u *)",", NULL) | |
8882 || (*l != NUL && l[STRLEN(l) - 1] == '\\')) | |
8883 break; | |
8884 l = ml_get_curline(); | |
8885 } | |
8886 | |
8887 /* | |
8888 * Doesn't look like anything interesting -- so just | |
8889 * use the indent of this line. | |
8890 * | |
8891 * Position the cursor over the rightmost paren, so that | |
8892 * matching it will take us back to the start of the line. | |
8893 */ | |
8894 find_last_paren(l, '(', ')'); | |
8895 | |
5442 | 8896 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
1550 | 8897 curwin->w_cursor = *trypos; |
7 | 8898 amount = get_indent(); /* XXX */ |
8899 break; | |
8900 } | |
8901 | |
8902 /* add extra indent for a comment */ | |
8903 if (cin_iscomment(theline)) | |
5438 | 8904 amount += curbuf->b_ind_comment; |
7 | 8905 |
8906 /* add extra indent if the previous line ended in a backslash: | |
8907 * "asdfasdf\ | |
8908 * here"; | |
8909 * char *foo = "asdf\ | |
8910 * here"; | |
8911 */ | |
8912 if (cur_curpos.lnum > 1) | |
8913 { | |
8914 l = ml_get(cur_curpos.lnum - 1); | |
8915 if (*l != NUL && l[STRLEN(l) - 1] == '\\') | |
8916 { | |
8917 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); | |
8918 if (cur_amount > 0) | |
8919 amount = cur_amount; | |
8920 else if (cur_amount == 0) | |
8921 amount += ind_continuation; | |
8922 } | |
8923 } | |
8924 } | |
8925 } | |
8926 | |
8927 theend: | |
8928 /* put the cursor back where it belongs */ | |
8929 curwin->w_cursor = cur_curpos; | |
8930 | |
8931 vim_free(linecopy); | |
8932 | |
8933 if (amount < 0) | |
8934 return 0; | |
8935 return amount; | |
8936 } | |
8937 | |
8938 static int | |
5442 | 8939 find_match(lookfor, ourscope) |
7 | 8940 int lookfor; |
8941 linenr_T ourscope; | |
8942 { | |
8943 char_u *look; | |
8944 pos_T *theirscope; | |
8945 char_u *mightbeif; | |
8946 int elselevel; | |
8947 int whilelevel; | |
8948 | |
8949 if (lookfor == LOOKFOR_IF) | |
8950 { | |
8951 elselevel = 1; | |
8952 whilelevel = 0; | |
8953 } | |
8954 else | |
8955 { | |
8956 elselevel = 0; | |
8957 whilelevel = 1; | |
8958 } | |
8959 | |
8960 curwin->w_cursor.col = 0; | |
8961 | |
8962 while (curwin->w_cursor.lnum > ourscope + 1) | |
8963 { | |
8964 curwin->w_cursor.lnum--; | |
8965 curwin->w_cursor.col = 0; | |
8966 | |
8967 look = cin_skipcomment(ml_get_curline()); | |
8968 if (cin_iselse(look) | |
8969 || cin_isif(look) | |
8970 || cin_isdo(look) /* XXX */ | |
5442 | 8971 || cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
7 | 8972 { |
8973 /* | |
8974 * if we've gone outside the braces entirely, | |
8975 * we must be out of scope... | |
8976 */ | |
5442 | 8977 theirscope = find_start_brace(); /* XXX */ |
7 | 8978 if (theirscope == NULL) |
8979 break; | |
8980 | |
8981 /* | |
8982 * and if the brace enclosing this is further | |
8983 * back than the one enclosing the else, we're | |
8984 * out of luck too. | |
8985 */ | |
8986 if (theirscope->lnum < ourscope) | |
8987 break; | |
8988 | |
8989 /* | |
8990 * and if they're enclosed in a *deeper* brace, | |
8991 * then we can ignore it because it's in a | |
8992 * different scope... | |
8993 */ | |
8994 if (theirscope->lnum > ourscope) | |
8995 continue; | |
8996 | |
8997 /* | |
8998 * if it was an "else" (that's not an "else if") | |
8999 * then we need to go back to another if, so | |
9000 * increment elselevel | |
9001 */ | |
9002 look = cin_skipcomment(ml_get_curline()); | |
9003 if (cin_iselse(look)) | |
9004 { | |
9005 mightbeif = cin_skipcomment(look + 4); | |
9006 if (!cin_isif(mightbeif)) | |
9007 ++elselevel; | |
9008 continue; | |
9009 } | |
9010 | |
9011 /* | |
9012 * if it was a "while" then we need to go back to | |
9013 * another "do", so increment whilelevel. XXX | |
9014 */ | |
5442 | 9015 if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
7 | 9016 { |
9017 ++whilelevel; | |
9018 continue; | |
9019 } | |
9020 | |
9021 /* If it's an "if" decrement elselevel */ | |
9022 look = cin_skipcomment(ml_get_curline()); | |
9023 if (cin_isif(look)) | |
9024 { | |
9025 elselevel--; | |
9026 /* | |
9027 * When looking for an "if" ignore "while"s that | |
9028 * get in the way. | |
9029 */ | |
9030 if (elselevel == 0 && lookfor == LOOKFOR_IF) | |
9031 whilelevel = 0; | |
9032 } | |
9033 | |
9034 /* If it's a "do" decrement whilelevel */ | |
9035 if (cin_isdo(look)) | |
9036 whilelevel--; | |
9037 | |
9038 /* | |
9039 * if we've used up all the elses, then | |
9040 * this must be the if that we want! | |
9041 * match the indent level of that if. | |
9042 */ | |
9043 if (elselevel <= 0 && whilelevel <= 0) | |
9044 { | |
9045 return OK; | |
9046 } | |
9047 } | |
9048 } | |
9049 return FAIL; | |
9050 } | |
9051 | |
9052 # if defined(FEAT_EVAL) || defined(PROTO) | |
9053 /* | |
9054 * Get indent level from 'indentexpr'. | |
9055 */ | |
9056 int | |
9057 get_expr_indent() | |
9058 { | |
9059 int indent; | |
4117 | 9060 pos_T save_pos; |
9061 colnr_T save_curswant; | |
9062 int save_set_curswant; | |
7 | 9063 int save_State; |
681 | 9064 int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
9065 OPT_LOCAL); | |
7 | 9066 |
4117 | 9067 /* Save and restore cursor position and curswant, in case it was changed |
9068 * via :normal commands */ | |
9069 save_pos = curwin->w_cursor; | |
9070 save_curswant = curwin->w_curswant; | |
9071 save_set_curswant = curwin->w_set_curswant; | |
7 | 9072 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); |
634 | 9073 if (use_sandbox) |
9074 ++sandbox; | |
9075 ++textlock; | |
7 | 9076 indent = eval_to_number(curbuf->b_p_inde); |
634 | 9077 if (use_sandbox) |
9078 --sandbox; | |
9079 --textlock; | |
7 | 9080 |
9081 /* Restore the cursor position so that 'indentexpr' doesn't need to. | |
9082 * Pretend to be in Insert mode, allow cursor past end of line for "o" | |
9083 * command. */ | |
9084 save_State = State; | |
9085 State = INSERT; | |
4117 | 9086 curwin->w_cursor = save_pos; |
9087 curwin->w_curswant = save_curswant; | |
9088 curwin->w_set_curswant = save_set_curswant; | |
7 | 9089 check_cursor(); |
9090 State = save_State; | |
9091 | |
9092 /* If there is an error, just keep the current indent. */ | |
9093 if (indent < 0) | |
9094 indent = get_indent(); | |
9095 | |
9096 return indent; | |
9097 } | |
9098 # endif | |
9099 | |
9100 #endif /* FEAT_CINDENT */ | |
9101 | |
9102 #if defined(FEAT_LISP) || defined(PROTO) | |
9103 | |
9104 static int lisp_match __ARGS((char_u *p)); | |
9105 | |
9106 static int | |
9107 lisp_match(p) | |
9108 char_u *p; | |
9109 { | |
9110 char_u buf[LSIZE]; | |
9111 int len; | |
5712 | 9112 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords; |
7 | 9113 |
9114 while (*word != NUL) | |
9115 { | |
9116 (void)copy_option_part(&word, buf, LSIZE, ","); | |
9117 len = (int)STRLEN(buf); | |
9118 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') | |
9119 return TRUE; | |
9120 } | |
9121 return FALSE; | |
9122 } | |
9123 | |
9124 /* | |
9125 * When 'p' is present in 'cpoptions, a Vi compatible method is used. | |
9126 * The incompatible newer method is quite a bit better at indenting | |
9127 * code in lisp-like languages than the traditional one; it's still | |
9128 * mostly heuristics however -- Dirk van Deun, dirk@rave.org | |
9129 * | |
9130 * TODO: | |
9131 * Findmatch() should be adapted for lisp, also to make showmatch | |
9132 * work correctly: now (v5.3) it seems all C/C++ oriented: | |
9133 * - it does not recognize the #\( and #\) notations as character literals | |
9134 * - it doesn't know about comments starting with a semicolon | |
9135 * - it incorrectly interprets '(' as a character literal | |
9136 * All this messes up get_lisp_indent in some rare cases. | |
14 | 9137 * Update from Sergey Khorev: |
9138 * I tried to fix the first two issues. | |
7 | 9139 */ |
9140 int | |
9141 get_lisp_indent() | |
9142 { | |
14 | 9143 pos_T *pos, realpos, paren; |
7 | 9144 int amount; |
9145 char_u *that; | |
9146 colnr_T col; | |
9147 colnr_T firsttry; | |
9148 int parencount, quotecount; | |
9149 int vi_lisp; | |
9150 | |
9151 /* Set vi_lisp to use the vi-compatible method */ | |
9152 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); | |
9153 | |
9154 realpos = curwin->w_cursor; | |
9155 curwin->w_cursor.col = 0; | |
9156 | |
14 | 9157 if ((pos = findmatch(NULL, '(')) == NULL) |
9158 pos = findmatch(NULL, '['); | |
9159 else | |
9160 { | |
9161 paren = *pos; | |
9162 pos = findmatch(NULL, '['); | |
9163 if (pos == NULL || ltp(pos, &paren)) | |
9164 pos = &paren; | |
9165 } | |
9166 if (pos != NULL) | |
7 | 9167 { |
9168 /* Extra trick: Take the indent of the first previous non-white | |
9169 * line that is at the same () level. */ | |
9170 amount = -1; | |
9171 parencount = 0; | |
9172 | |
9173 while (--curwin->w_cursor.lnum >= pos->lnum) | |
9174 { | |
9175 if (linewhite(curwin->w_cursor.lnum)) | |
9176 continue; | |
9177 for (that = ml_get_curline(); *that != NUL; ++that) | |
9178 { | |
9179 if (*that == ';') | |
9180 { | |
9181 while (*(that + 1) != NUL) | |
9182 ++that; | |
9183 continue; | |
9184 } | |
9185 if (*that == '\\') | |
9186 { | |
9187 if (*(that + 1) != NUL) | |
9188 ++that; | |
9189 continue; | |
9190 } | |
9191 if (*that == '"' && *(that + 1) != NUL) | |
9192 { | |
983 | 9193 while (*++that && *that != '"') |
9194 { | |
9195 /* skipping escaped characters in the string */ | |
9196 if (*that == '\\') | |
9197 { | |
9198 if (*++that == NUL) | |
9199 break; | |
9200 if (that[1] == NUL) | |
9201 { | |
9202 ++that; | |
9203 break; | |
9204 } | |
9205 } | |
9206 } | |
7 | 9207 } |
14 | 9208 if (*that == '(' || *that == '[') |
7 | 9209 ++parencount; |
14 | 9210 else if (*that == ')' || *that == ']') |
7 | 9211 --parencount; |
9212 } | |
9213 if (parencount == 0) | |
9214 { | |
9215 amount = get_indent(); | |
9216 break; | |
9217 } | |
9218 } | |
9219 | |
9220 if (amount == -1) | |
9221 { | |
9222 curwin->w_cursor.lnum = pos->lnum; | |
9223 curwin->w_cursor.col = pos->col; | |
9224 col = pos->col; | |
9225 | |
9226 that = ml_get_curline(); | |
9227 | |
9228 if (vi_lisp && get_indent() == 0) | |
9229 amount = 2; | |
9230 else | |
9231 { | |
5995 | 9232 char_u *line = that; |
9233 | |
7 | 9234 amount = 0; |
9235 while (*that && col) | |
9236 { | |
5995 | 9237 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount); |
7 | 9238 col--; |
9239 } | |
9240 | |
9241 /* | |
9242 * Some keywords require "body" indenting rules (the | |
9243 * non-standard-lisp ones are Scheme special forms): | |
9244 * | |
9245 * (let ((a 1)) instead (let ((a 1)) | |
9246 * (...)) of (...)) | |
9247 */ | |
9248 | |
14 | 9249 if (!vi_lisp && (*that == '(' || *that == '[') |
9250 && lisp_match(that + 1)) | |
7 | 9251 amount += 2; |
9252 else | |
9253 { | |
9254 that++; | |
9255 amount++; | |
9256 firsttry = amount; | |
9257 | |
9258 while (vim_iswhite(*that)) | |
9259 { | |
5995 | 9260 amount += lbr_chartabsize(line, that, (colnr_T)amount); |
7 | 9261 ++that; |
9262 } | |
9263 | |
9264 if (*that && *that != ';') /* not a comment line */ | |
9265 { | |
1532 | 9266 /* test *that != '(' to accommodate first let/do |
7 | 9267 * argument if it is more than one line */ |
14 | 9268 if (!vi_lisp && *that != '(' && *that != '[') |
7 | 9269 firsttry++; |
9270 | |
9271 parencount = 0; | |
9272 quotecount = 0; | |
9273 | |
9274 if (vi_lisp | |
9275 || (*that != '"' | |
9276 && *that != '\'' | |
9277 && *that != '#' | |
9278 && (*that < '0' || *that > '9'))) | |
9279 { | |
9280 while (*that | |
9281 && (!vim_iswhite(*that) | |
9282 || quotecount | |
9283 || parencount) | |
14 | 9284 && (!((*that == '(' || *that == '[') |
7 | 9285 && !quotecount |
9286 && !parencount | |
9287 && vi_lisp))) | |
9288 { | |
9289 if (*that == '"') | |
9290 quotecount = !quotecount; | |
14 | 9291 if ((*that == '(' || *that == '[') |
9292 && !quotecount) | |
7 | 9293 ++parencount; |
14 | 9294 if ((*that == ')' || *that == ']') |
9295 && !quotecount) | |
7 | 9296 --parencount; |
9297 if (*that == '\\' && *(that+1) != NUL) | |
5995 | 9298 amount += lbr_chartabsize_adv( |
9299 line, &that, (colnr_T)amount); | |
9300 amount += lbr_chartabsize_adv( | |
9301 line, &that, (colnr_T)amount); | |
7 | 9302 } |
9303 } | |
9304 while (vim_iswhite(*that)) | |
9305 { | |
5995 | 9306 amount += lbr_chartabsize( |
9307 line, that, (colnr_T)amount); | |
7 | 9308 that++; |
9309 } | |
9310 if (!*that || *that == ';') | |
9311 amount = firsttry; | |
9312 } | |
9313 } | |
9314 } | |
9315 } | |
9316 } | |
9317 else | |
14 | 9318 amount = 0; /* no matching '(' or '[' found, use zero indent */ |
7 | 9319 |
9320 curwin->w_cursor = realpos; | |
9321 | |
9322 return amount; | |
9323 } | |
9324 #endif /* FEAT_LISP */ | |
9325 | |
9326 void | |
9327 prepare_to_exit() | |
9328 { | |
39 | 9329 #if defined(SIGHUP) && defined(SIG_IGN) |
9330 /* Ignore SIGHUP, because a dropped connection causes a read error, which | |
9331 * makes Vim exit and then handling SIGHUP causes various reentrance | |
9332 * problems. */ | |
36 | 9333 signal(SIGHUP, SIG_IGN); |
9334 #endif | |
9335 | |
7 | 9336 #ifdef FEAT_GUI |
9337 if (gui.in_use) | |
9338 { | |
9339 gui.dying = TRUE; | |
9340 out_trash(); /* trash any pending output */ | |
9341 } | |
9342 else | |
9343 #endif | |
9344 { | |
9345 windgoto((int)Rows - 1, 0); | |
9346 | |
9347 /* | |
9348 * Switch terminal mode back now, so messages end up on the "normal" | |
9349 * screen (if there are two screens). | |
9350 */ | |
9351 settmode(TMODE_COOK); | |
9352 #ifdef WIN3264 | |
9353 if (can_end_termcap_mode(FALSE) == TRUE) | |
9354 #endif | |
9355 stoptermcap(); | |
9356 out_flush(); | |
9357 } | |
9358 } | |
9359 | |
9360 /* | |
9361 * Preserve files and exit. | |
9362 * When called IObuff must contain a message. | |
5338 | 9363 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe |
9364 * functions, such as allocating memory. | |
7 | 9365 */ |
9366 void | |
9367 preserve_exit() | |
9368 { | |
9369 buf_T *buf; | |
9370 | |
9371 prepare_to_exit(); | |
9372 | |
625 | 9373 /* Setting this will prevent free() calls. That avoids calling free() |
9374 * recursively when free() was invoked with a bad pointer. */ | |
9375 really_exiting = TRUE; | |
9376 | |
7 | 9377 out_str(IObuff); |
9378 screen_start(); /* don't know where cursor is now */ | |
9379 out_flush(); | |
9380 | |
9381 ml_close_notmod(); /* close all not-modified buffers */ | |
9382 | |
9383 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
9384 { | |
9385 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) | |
9386 { | |
5338 | 9387 OUT_STR("Vim: preserving files...\n"); |
7 | 9388 screen_start(); /* don't know where cursor is now */ |
9389 out_flush(); | |
9390 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ | |
9391 break; | |
9392 } | |
9393 } | |
9394 | |
9395 ml_close_all(FALSE); /* close all memfiles, without deleting */ | |
9396 | |
5338 | 9397 OUT_STR("Vim: Finished.\n"); |
7 | 9398 |
9399 getout(1); | |
9400 } | |
9401 | |
9402 /* | |
9403 * return TRUE if "fname" exists. | |
9404 */ | |
9405 int | |
9406 vim_fexists(fname) | |
9407 char_u *fname; | |
9408 { | |
9409 struct stat st; | |
9410 | |
9411 if (mch_stat((char *)fname, &st)) | |
9412 return FALSE; | |
9413 return TRUE; | |
9414 } | |
9415 | |
9416 /* | |
9417 * Check for CTRL-C pressed, but only once in a while. | |
9418 * Should be used instead of ui_breakcheck() for functions that check for | |
9419 * each line in the file. Calling ui_breakcheck() each time takes too much | |
9420 * time, because it can be a system call. | |
9421 */ | |
9422 | |
9423 #ifndef BREAKCHECK_SKIP | |
9424 # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ | |
9425 # define BREAKCHECK_SKIP 200 | |
9426 # else | |
9427 # define BREAKCHECK_SKIP 32 | |
9428 # endif | |
9429 #endif | |
9430 | |
9431 static int breakcheck_count = 0; | |
9432 | |
9433 void | |
9434 line_breakcheck() | |
9435 { | |
9436 if (++breakcheck_count >= BREAKCHECK_SKIP) | |
9437 { | |
9438 breakcheck_count = 0; | |
9439 ui_breakcheck(); | |
9440 } | |
9441 } | |
9442 | |
9443 /* | |
9444 * Like line_breakcheck() but check 10 times less often. | |
9445 */ | |
9446 void | |
9447 fast_breakcheck() | |
9448 { | |
9449 if (++breakcheck_count >= BREAKCHECK_SKIP * 10) | |
9450 { | |
9451 breakcheck_count = 0; | |
9452 ui_breakcheck(); | |
9453 } | |
9454 } | |
9455 | |
9456 /* | |
2016 | 9457 * Invoke expand_wildcards() for one pattern. |
9458 * Expand items like "%:h" before the expansion. | |
9459 * Returns OK or FAIL. | |
9460 */ | |
9461 int | |
9462 expand_wildcards_eval(pat, num_file, file, flags) | |
9463 char_u **pat; /* pointer to input pattern */ | |
9464 int *num_file; /* resulting number of files */ | |
9465 char_u ***file; /* array of resulting files */ | |
9466 int flags; /* EW_DIR, etc. */ | |
9467 { | |
9468 int ret = FAIL; | |
9469 char_u *eval_pat = NULL; | |
9470 char_u *exp_pat = *pat; | |
9471 char_u *ignored_msg; | |
9472 int usedlen; | |
9473 | |
9474 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') | |
9475 { | |
9476 ++emsg_off; | |
9477 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, | |
9478 NULL, &ignored_msg, NULL); | |
9479 --emsg_off; | |
9480 if (eval_pat != NULL) | |
9481 exp_pat = concat_str(eval_pat, exp_pat + usedlen); | |
9482 } | |
9483 | |
9484 if (exp_pat != NULL) | |
9485 ret = expand_wildcards(1, &exp_pat, num_file, file, flags); | |
9486 | |
9487 if (eval_pat != NULL) | |
9488 { | |
9489 vim_free(exp_pat); | |
9490 vim_free(eval_pat); | |
9491 } | |
9492 | |
9493 return ret; | |
9494 } | |
9495 | |
9496 /* | |
7 | 9497 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
9498 * 'wildignore'. | |
2354
f4440cdd59ae
Fixed: crash with ":find " completion, using uninitialized count.
Bram Moolenaar <bram@vim.org>
parents:
2328
diff
changeset
|
9499 * Returns OK or FAIL. When FAIL then "num_file" won't be set. |
7 | 9500 */ |
9501 int | |
9502 expand_wildcards(num_pat, pat, num_file, file, flags) | |
9503 int num_pat; /* number of input patterns */ | |
9504 char_u **pat; /* array of input patterns */ | |
9505 int *num_file; /* resulting number of files */ | |
9506 char_u ***file; /* array of resulting files */ | |
9507 int flags; /* EW_DIR, etc. */ | |
9508 { | |
9509 int retval; | |
9510 int i, j; | |
9511 char_u *p; | |
9512 int non_suf_match; /* number without matching suffix */ | |
9513 | |
9514 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags); | |
9515 | |
9516 /* 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
|
9517 if ((flags & EW_KEEPALL) || retval == FAIL) |
7 | 9518 return retval; |
9519 | |
9520 #ifdef FEAT_WILDIGN | |
9521 /* | |
9522 * Remove names that match 'wildignore'. | |
9523 */ | |
9524 if (*p_wig) | |
9525 { | |
9526 char_u *ffname; | |
9527 | |
9528 /* check all files in (*file)[] */ | |
9529 for (i = 0; i < *num_file; ++i) | |
9530 { | |
9531 ffname = FullName_save((*file)[i], FALSE); | |
9532 if (ffname == NULL) /* out of memory */ | |
9533 break; | |
9534 # ifdef VMS | |
9535 vms_remove_version(ffname); | |
9536 # endif | |
9537 if (match_file_list(p_wig, (*file)[i], ffname)) | |
9538 { | |
9539 /* remove this matching file from the list */ | |
9540 vim_free((*file)[i]); | |
9541 for (j = i; j + 1 < *num_file; ++j) | |
9542 (*file)[j] = (*file)[j + 1]; | |
9543 --*num_file; | |
9544 --i; | |
9545 } | |
9546 vim_free(ffname); | |
9547 } | |
9548 } | |
9549 #endif | |
9550 | |
9551 /* | |
9552 * Move the names where 'suffixes' match to the end. | |
9553 */ | |
9554 if (*num_file > 1) | |
9555 { | |
9556 non_suf_match = 0; | |
9557 for (i = 0; i < *num_file; ++i) | |
9558 { | |
9559 if (!match_suffix((*file)[i])) | |
9560 { | |
9561 /* | |
9562 * Move the name without matching suffix to the front | |
9563 * of the list. | |
9564 */ | |
9565 p = (*file)[i]; | |
9566 for (j = i; j > non_suf_match; --j) | |
9567 (*file)[j] = (*file)[j - 1]; | |
9568 (*file)[non_suf_match++] = p; | |
9569 } | |
9570 } | |
9571 } | |
9572 | |
9573 return retval; | |
9574 } | |
9575 | |
9576 /* | |
9577 * Return TRUE if "fname" matches with an entry in 'suffixes'. | |
9578 */ | |
9579 int | |
9580 match_suffix(fname) | |
9581 char_u *fname; | |
9582 { | |
9583 int fnamelen, setsuflen; | |
9584 char_u *setsuf; | |
9585 #define MAXSUFLEN 30 /* maximum length of a file suffix */ | |
9586 char_u suf_buf[MAXSUFLEN]; | |
9587 | |
9588 fnamelen = (int)STRLEN(fname); | |
9589 setsuflen = 0; | |
9590 for (setsuf = p_su; *setsuf; ) | |
9591 { | |
9592 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); | |
1937 | 9593 if (setsuflen == 0) |
9594 { | |
9595 char_u *tail = gettail(fname); | |
9596 | |
9597 /* empty entry: match name without a '.' */ | |
9598 if (vim_strchr(tail, '.') == NULL) | |
9599 { | |
9600 setsuflen = 1; | |
9601 break; | |
9602 } | |
9603 } | |
9604 else | |
9605 { | |
9606 if (fnamelen >= setsuflen | |
9607 && fnamencmp(suf_buf, fname + fnamelen - setsuflen, | |
9608 (size_t)setsuflen) == 0) | |
9609 break; | |
9610 setsuflen = 0; | |
9611 } | |
7 | 9612 } |
9613 return (setsuflen != 0); | |
9614 } | |
9615 | |
9616 #if !defined(NO_EXPANDPATH) || defined(PROTO) | |
9617 | |
9618 # ifdef VIM_BACKTICK | |
9619 static int vim_backtick __ARGS((char_u *p)); | |
9620 static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags)); | |
9621 # endif | |
9622 | |
9623 # if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264) | |
9624 /* | |
9625 * File name expansion code for MS-DOS, Win16 and Win32. It's here because | |
9626 * it's shared between these systems. | |
9627 */ | |
9628 # if defined(DJGPP) || defined(PROTO) | |
9629 # define _cdecl /* DJGPP doesn't have this */ | |
9630 # else | |
9631 # ifdef __BORLANDC__ | |
9632 # define _cdecl _RTLENTRYF | |
9633 # endif | |
9634 # endif | |
9635 | |
9636 /* | |
9637 * comparison function for qsort in dos_expandpath() | |
9638 */ | |
9639 static int _cdecl | |
9640 pstrcmp(const void *a, const void *b) | |
9641 { | |
39 | 9642 return (pathcmp(*(char **)a, *(char **)b, -1)); |
7 | 9643 } |
9644 | |
9645 # ifndef WIN3264 | |
9646 static void | |
9647 namelowcpy( | |
9648 char_u *d, | |
9649 char_u *s) | |
9650 { | |
9651 # ifdef DJGPP | |
9652 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */ | |
9653 while (*s) | |
9654 *d++ = *s++; | |
9655 else | |
9656 # endif | |
9657 while (*s) | |
9658 *d++ = TOLOWER_LOC(*s++); | |
9659 *d = NUL; | |
9660 } | |
9661 # endif | |
9662 | |
9663 /* | |
445 | 9664 * Recursively expand one path component into all matching files and/or |
9665 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
7 | 9666 * Return the number of matches found. |
9667 * "path" has backslashes before chars that are not to be expanded, starting | |
9668 * at "path[wildoff]". | |
445 | 9669 * Return the number of matches found. |
9670 * NOTE: much of this is identical to unix_expandpath(), keep in sync! | |
7 | 9671 */ |
9672 static int | |
9673 dos_expandpath( | |
9674 garray_T *gap, | |
9675 char_u *path, | |
9676 int wildoff, | |
445 | 9677 int flags, /* EW_* flags */ |
1224 | 9678 int didstar) /* expanded "**" once already */ |
445 | 9679 { |
9680 char_u *buf; | |
9681 char_u *path_end; | |
9682 char_u *p, *s, *e; | |
9683 int start_len = gap->ga_len; | |
9684 char_u *pat; | |
9685 regmatch_T regmatch; | |
9686 int starts_with_dot; | |
9687 int matches; | |
9688 int len; | |
9689 int starstar = FALSE; | |
9690 static int stardepth = 0; /* depth for "**" expansion */ | |
7 | 9691 #ifdef WIN3264 |
9692 WIN32_FIND_DATA fb; | |
9693 HANDLE hFind = (HANDLE)0; | |
9694 # ifdef FEAT_MBYTE | |
9695 WIN32_FIND_DATAW wfb; | |
9696 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ | |
9697 # endif | |
9698 #else | |
9699 struct ffblk fb; | |
9700 #endif | |
9701 char_u *matchname; | |
445 | 9702 int ok; |
9703 | |
9704 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
9705 if (stardepth > 0) | |
9706 { | |
9707 ui_breakcheck(); | |
9708 if (got_int) | |
9709 return 0; | |
9710 } | |
7 | 9711 |
9712 /* make room for file name */ | |
445 | 9713 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
7 | 9714 if (buf == NULL) |
9715 return 0; | |
9716 | |
9717 /* | |
9718 * Find the first part in the path name that contains a wildcard or a ~1. | |
9719 * Copy it into buf, including the preceding characters. | |
9720 */ | |
9721 p = buf; | |
9722 s = buf; | |
9723 e = NULL; | |
9724 path_end = path; | |
9725 while (*path_end != NUL) | |
9726 { | |
9727 /* May ignore a wildcard that has a backslash before it; it will | |
9728 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
9729 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
9730 *p++ = *path_end++; | |
9731 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') | |
9732 { | |
9733 if (e != NULL) | |
9734 break; | |
9735 s = p + 1; | |
9736 } | |
9737 else if (path_end >= path + wildoff | |
9738 && vim_strchr((char_u *)"*?[~", *path_end) != NULL) | |
9739 e = p; | |
9740 #ifdef FEAT_MBYTE | |
9741 if (has_mbyte) | |
9742 { | |
474 | 9743 len = (*mb_ptr2len)(path_end); |
7 | 9744 STRNCPY(p, path_end, len); |
9745 p += len; | |
9746 path_end += len; | |
9747 } | |
9748 else | |
9749 #endif | |
9750 *p++ = *path_end++; | |
9751 } | |
9752 e = p; | |
9753 *e = NUL; | |
9754 | |
9755 /* now we have one wildcard component between s and e */ | |
9756 /* Remove backslashes between "wildoff" and the start of the wildcard | |
9757 * component. */ | |
9758 for (p = buf + wildoff; p < s; ++p) | |
9759 if (rem_backslash(p)) | |
9760 { | |
1624 | 9761 STRMOVE(p, p + 1); |
7 | 9762 --e; |
9763 --s; | |
9764 } | |
9765 | |
445 | 9766 /* Check for "**" between "s" and "e". */ |
9767 for (p = s; p < e; ++p) | |
9768 if (p[0] == '*' && p[1] == '*') | |
9769 starstar = TRUE; | |
9770 | |
7 | 9771 starts_with_dot = (*s == '.'); |
9772 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
9773 if (pat == NULL) | |
9774 { | |
9775 vim_free(buf); | |
9776 return 0; | |
9777 } | |
9778 | |
9779 /* compile the regexp into a program */ | |
3261 | 9780 if (flags & (EW_NOERROR | EW_NOTWILD)) |
2966 | 9781 ++emsg_silent; |
7 | 9782 regmatch.rm_ic = TRUE; /* Always ignore case */ |
9783 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
3261 | 9784 if (flags & (EW_NOERROR | EW_NOTWILD)) |
2966 | 9785 --emsg_silent; |
7 | 9786 vim_free(pat); |
9787 | |
3261 | 9788 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
7 | 9789 { |
9790 vim_free(buf); | |
9791 return 0; | |
9792 } | |
9793 | |
9794 /* remember the pattern or file name being looked for */ | |
9795 matchname = vim_strsave(s); | |
9796 | |
445 | 9797 /* If "**" is by itself, this is the first time we encounter it and more |
9798 * is following then find matches without any directory. */ | |
9799 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
9800 && *path_end == '/') | |
9801 { | |
9802 STRCPY(s, path_end + 1); | |
9803 ++stardepth; | |
9804 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
9805 --stardepth; | |
9806 } | |
9807 | |
7 | 9808 /* Scan all files in the directory with "dir/ *.*" */ |
9809 STRCPY(s, "*.*"); | |
9810 #ifdef WIN3264 | |
9811 # ifdef FEAT_MBYTE | |
9812 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
9813 { | |
9814 /* The active codepage differs from 'encoding'. Attempt using the | |
9815 * wide function. If it fails because it is not implemented fall back | |
9816 * to the non-wide version (for Windows 98) */ | |
1752 | 9817 wn = enc_to_utf16(buf, NULL); |
7 | 9818 if (wn != NULL) |
9819 { | |
9820 hFind = FindFirstFileW(wn, &wfb); | |
9821 if (hFind == INVALID_HANDLE_VALUE | |
9822 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) | |
9823 { | |
9824 vim_free(wn); | |
9825 wn = NULL; | |
9826 } | |
9827 } | |
9828 } | |
9829 | |
9830 if (wn == NULL) | |
9831 # endif | |
9832 hFind = FindFirstFile(buf, &fb); | |
9833 ok = (hFind != INVALID_HANDLE_VALUE); | |
9834 #else | |
9835 /* If we are expanding wildcards we try both files and directories */ | |
9836 ok = (findfirst((char *)buf, &fb, | |
9837 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); | |
9838 #endif | |
9839 | |
9840 while (ok) | |
9841 { | |
9842 #ifdef WIN3264 | |
9843 # ifdef FEAT_MBYTE | |
9844 if (wn != NULL) | |
1752 | 9845 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */ |
7 | 9846 else |
9847 # endif | |
9848 p = (char_u *)fb.cFileName; | |
9849 #else | |
9850 p = (char_u *)fb.ff_name; | |
9851 #endif | |
9852 /* Ignore entries starting with a dot, unless when asked for. Accept | |
9853 * all entries found with "matchname". */ | |
9854 if ((p[0] != '.' || starts_with_dot) | |
9855 && (matchname == NULL | |
3261 | 9856 || (regmatch.regprog != NULL |
9857 && vim_regexec(®match, p, (colnr_T)0)) | |
2984 | 9858 || ((flags & EW_NOTWILD) |
9859 && fnamencmp(path + (s - buf), p, e - s) == 0))) | |
7 | 9860 { |
9861 #ifdef WIN3264 | |
9862 STRCPY(s, p); | |
9863 #else | |
9864 namelowcpy(s, p); | |
9865 #endif | |
9866 len = (int)STRLEN(buf); | |
445 | 9867 |
9868 if (starstar && stardepth < 100) | |
9869 { | |
9870 /* For "**" in the pattern first go deeper in the tree to | |
9871 * find matches. */ | |
9872 STRCPY(buf + len, "/**"); | |
9873 STRCPY(buf + len + 3, path_end); | |
9874 ++stardepth; | |
9875 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); | |
9876 --stardepth; | |
9877 } | |
9878 | |
7 | 9879 STRCPY(buf + len, path_end); |
9880 if (mch_has_exp_wildcard(path_end)) | |
9881 { | |
9882 /* need to expand another component of the path */ | |
9883 /* remove backslashes for the remaining components only */ | |
445 | 9884 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
7 | 9885 } |
9886 else | |
9887 { | |
9888 /* no more wildcards, check if there is a match */ | |
9889 /* remove backslashes for the remaining components only */ | |
9890 if (*path_end != 0) | |
9891 backslash_halve(buf + len + 1); | |
9892 if (mch_getperm(buf) >= 0) /* add existing file */ | |
9893 addfile(gap, buf, flags); | |
9894 } | |
9895 } | |
9896 | |
9897 #ifdef WIN3264 | |
9898 # ifdef FEAT_MBYTE | |
9899 if (wn != NULL) | |
9900 { | |
9901 vim_free(p); | |
9902 ok = FindNextFileW(hFind, &wfb); | |
9903 } | |
9904 else | |
9905 # endif | |
9906 ok = FindNextFile(hFind, &fb); | |
9907 #else | |
9908 ok = (findnext(&fb) == 0); | |
9909 #endif | |
9910 | |
9911 /* If no more matches and no match was used, try expanding the name | |
9912 * itself. Finds the long name of a short filename. */ | |
9913 if (!ok && matchname != NULL && gap->ga_len == start_len) | |
9914 { | |
9915 STRCPY(s, matchname); | |
9916 #ifdef WIN3264 | |
9917 FindClose(hFind); | |
9918 # ifdef FEAT_MBYTE | |
9919 if (wn != NULL) | |
9920 { | |
9921 vim_free(wn); | |
1752 | 9922 wn = enc_to_utf16(buf, NULL); |
7 | 9923 if (wn != NULL) |
9924 hFind = FindFirstFileW(wn, &wfb); | |
9925 } | |
9926 if (wn == NULL) | |
9927 # endif | |
9928 hFind = FindFirstFile(buf, &fb); | |
9929 ok = (hFind != INVALID_HANDLE_VALUE); | |
9930 #else | |
9931 ok = (findfirst((char *)buf, &fb, | |
9932 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); | |
9933 #endif | |
9934 vim_free(matchname); | |
9935 matchname = NULL; | |
9936 } | |
9937 } | |
9938 | |
9939 #ifdef WIN3264 | |
9940 FindClose(hFind); | |
9941 # ifdef FEAT_MBYTE | |
9942 vim_free(wn); | |
9943 # endif | |
9944 #endif | |
9945 vim_free(buf); | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
9946 vim_regfree(regmatch.regprog); |
7 | 9947 vim_free(matchname); |
9948 | |
9949 matches = gap->ga_len - start_len; | |
9950 if (matches > 0) | |
9951 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, | |
9952 sizeof(char_u *), pstrcmp); | |
9953 return matches; | |
9954 } | |
9955 | |
9956 int | |
9957 mch_expandpath( | |
9958 garray_T *gap, | |
9959 char_u *path, | |
9960 int flags) /* EW_* flags */ | |
9961 { | |
445 | 9962 return dos_expandpath(gap, path, 0, flags, FALSE); |
7 | 9963 } |
9964 # endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */ | |
9965 | |
445 | 9966 #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
9967 || defined(PROTO) | |
9968 /* | |
9969 * Unix style wildcard expansion code. | |
9970 * It's here because it's used both for Unix and Mac. | |
9971 */ | |
9972 static int pstrcmp __ARGS((const void *, const void *)); | |
9973 | |
9974 static int | |
9975 pstrcmp(a, b) | |
9976 const void *a, *b; | |
9977 { | |
9978 return (pathcmp(*(char **)a, *(char **)b, -1)); | |
9979 } | |
9980 | |
9981 /* | |
9982 * Recursively expand one path component into all matching files and/or | |
9983 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
9984 * "path" has backslashes before chars that are not to be expanded, starting | |
9985 * at "path + wildoff". | |
9986 * Return the number of matches found. | |
9987 * NOTE: much of this is identical to dos_expandpath(), keep in sync! | |
9988 */ | |
9989 int | |
9990 unix_expandpath(gap, path, wildoff, flags, didstar) | |
9991 garray_T *gap; | |
9992 char_u *path; | |
9993 int wildoff; | |
9994 int flags; /* EW_* flags */ | |
9995 int didstar; /* expanded "**" once already */ | |
9996 { | |
9997 char_u *buf; | |
9998 char_u *path_end; | |
9999 char_u *p, *s, *e; | |
10000 int start_len = gap->ga_len; | |
10001 char_u *pat; | |
10002 regmatch_T regmatch; | |
10003 int starts_with_dot; | |
10004 int matches; | |
10005 int len; | |
10006 int starstar = FALSE; | |
10007 static int stardepth = 0; /* depth for "**" expansion */ | |
10008 | |
10009 DIR *dirp; | |
10010 struct dirent *dp; | |
10011 | |
10012 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
10013 if (stardepth > 0) | |
10014 { | |
10015 ui_breakcheck(); | |
10016 if (got_int) | |
10017 return 0; | |
10018 } | |
10019 | |
10020 /* make room for file name */ | |
10021 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); | |
10022 if (buf == NULL) | |
10023 return 0; | |
10024 | |
10025 /* | |
10026 * Find the first part in the path name that contains a wildcard. | |
3505 | 10027 * When EW_ICASE is set every letter is considered to be a wildcard. |
445 | 10028 * Copy it into "buf", including the preceding characters. |
10029 */ | |
10030 p = buf; | |
10031 s = buf; | |
10032 e = NULL; | |
10033 path_end = path; | |
10034 while (*path_end != NUL) | |
10035 { | |
10036 /* May ignore a wildcard that has a backslash before it; it will | |
10037 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
10038 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
10039 *p++ = *path_end++; | |
10040 else if (*path_end == '/') | |
10041 { | |
10042 if (e != NULL) | |
10043 break; | |
10044 s = p + 1; | |
10045 } | |
10046 else if (path_end >= path + wildoff | |
3505 | 10047 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
4242 | 10048 || (!p_fic && (flags & EW_ICASE) |
10049 && isalpha(PTR2CHAR(path_end))))) | |
445 | 10050 e = p; |
10051 #ifdef FEAT_MBYTE | |
10052 if (has_mbyte) | |
10053 { | |
474 | 10054 len = (*mb_ptr2len)(path_end); |
445 | 10055 STRNCPY(p, path_end, len); |
10056 p += len; | |
10057 path_end += len; | |
10058 } | |
10059 else | |
10060 #endif | |
10061 *p++ = *path_end++; | |
10062 } | |
10063 e = p; | |
10064 *e = NUL; | |
10065 | |
2984 | 10066 /* Now we have one wildcard component between "s" and "e". */ |
445 | 10067 /* Remove backslashes between "wildoff" and the start of the wildcard |
10068 * component. */ | |
10069 for (p = buf + wildoff; p < s; ++p) | |
10070 if (rem_backslash(p)) | |
10071 { | |
1624 | 10072 STRMOVE(p, p + 1); |
445 | 10073 --e; |
10074 --s; | |
10075 } | |
10076 | |
10077 /* Check for "**" between "s" and "e". */ | |
10078 for (p = s; p < e; ++p) | |
10079 if (p[0] == '*' && p[1] == '*') | |
10080 starstar = TRUE; | |
10081 | |
10082 /* convert the file pattern to a regexp pattern */ | |
10083 starts_with_dot = (*s == '.'); | |
10084 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
10085 if (pat == NULL) | |
10086 { | |
10087 vim_free(buf); | |
10088 return 0; | |
10089 } | |
10090 | |
10091 /* compile the regexp into a program */ | |
2652 | 10092 if (flags & EW_ICASE) |
10093 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ | |
10094 else | |
4242 | 10095 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ |
3261 | 10096 if (flags & (EW_NOERROR | EW_NOTWILD)) |
10097 ++emsg_silent; | |
445 | 10098 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
3261 | 10099 if (flags & (EW_NOERROR | EW_NOTWILD)) |
10100 --emsg_silent; | |
445 | 10101 vim_free(pat); |
10102 | |
3261 | 10103 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
445 | 10104 { |
10105 vim_free(buf); | |
10106 return 0; | |
10107 } | |
10108 | |
10109 /* If "**" is by itself, this is the first time we encounter it and more | |
10110 * is following then find matches without any directory. */ | |
10111 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
10112 && *path_end == '/') | |
10113 { | |
10114 STRCPY(s, path_end + 1); | |
10115 ++stardepth; | |
10116 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
10117 --stardepth; | |
10118 } | |
10119 | |
10120 /* open the directory for scanning */ | |
10121 *s = NUL; | |
10122 dirp = opendir(*buf == NUL ? "." : (char *)buf); | |
10123 | |
10124 /* Find all matching entries */ | |
10125 if (dirp != NULL) | |
10126 { | |
10127 for (;;) | |
10128 { | |
10129 dp = readdir(dirp); | |
10130 if (dp == NULL) | |
10131 break; | |
10132 if ((dp->d_name[0] != '.' || starts_with_dot) | |
3261 | 10133 && ((regmatch.regprog != NULL && vim_regexec(®match, |
10134 (char_u *)dp->d_name, (colnr_T)0)) | |
2984 | 10135 || ((flags & EW_NOTWILD) |
10136 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) | |
445 | 10137 { |
10138 STRCPY(s, dp->d_name); | |
10139 len = STRLEN(buf); | |
10140 | |
10141 if (starstar && stardepth < 100) | |
10142 { | |
10143 /* For "**" in the pattern first go deeper in the tree to | |
10144 * find matches. */ | |
10145 STRCPY(buf + len, "/**"); | |
10146 STRCPY(buf + len + 3, path_end); | |
10147 ++stardepth; | |
10148 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); | |
10149 --stardepth; | |
10150 } | |
10151 | |
10152 STRCPY(buf + len, path_end); | |
10153 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ | |
10154 { | |
10155 /* need to expand another component of the path */ | |
10156 /* remove backslashes for the remaining components only */ | |
10157 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); | |
10158 } | |
10159 else | |
10160 { | |
10161 /* no more wildcards, check if there is a match */ | |
10162 /* remove backslashes for the remaining components only */ | |
10163 if (*path_end != NUL) | |
10164 backslash_halve(buf + len + 1); | |
10165 if (mch_getperm(buf) >= 0) /* add existing file */ | |
10166 { | |
768 | 10167 #ifdef MACOS_CONVERT |
445 | 10168 size_t precomp_len = STRLEN(buf)+1; |
10169 char_u *precomp_buf = | |
10170 mac_precompose_path(buf, precomp_len, &precomp_len); | |
768 | 10171 |
445 | 10172 if (precomp_buf) |
10173 { | |
10174 mch_memmove(buf, precomp_buf, precomp_len); | |
10175 vim_free(precomp_buf); | |
10176 } | |
10177 #endif | |
10178 addfile(gap, buf, flags); | |
10179 } | |
10180 } | |
10181 } | |
10182 } | |
10183 | |
10184 closedir(dirp); | |
10185 } | |
10186 | |
10187 vim_free(buf); | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
10188 vim_regfree(regmatch.regprog); |
445 | 10189 |
10190 matches = gap->ga_len - start_len; | |
10191 if (matches > 0) | |
10192 qsort(((char_u **)gap->ga_data) + start_len, matches, | |
10193 sizeof(char_u *), pstrcmp); | |
10194 return matches; | |
10195 } | |
10196 #endif | |
10197 | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10198 #if defined(FEAT_SEARCHPATH) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10199 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
|
10200 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
|
10201 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
|
10202 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
|
10203 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
|
10204 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
|
10205 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10206 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10207 * 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
|
10208 * 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
|
10209 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10210 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10211 find_previous_pathsep(path, psep) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10212 char_u *path; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10213 char_u **psep; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10214 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10215 /* skip the current separator */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10216 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
|
10217 --*psep; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10218 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10219 /* 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
|
10220 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
|
10221 { |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10222 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
|
10223 return OK; |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10224 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
|
10225 } |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10226 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10227 return FAIL; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10228 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10229 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10230 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10231 * 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
|
10232 * "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
|
10233 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10234 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10235 is_unique(maybe_unique, gap, i) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10236 char_u *maybe_unique; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10237 garray_T *gap; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10238 int i; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10239 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10240 int j; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10241 int candidate_len; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10242 int other_path_len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10243 char_u **other_paths = (char_u **)gap->ga_data; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10244 char_u *rival; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10245 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10246 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
|
10247 { |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10248 if (j == i) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10249 continue; /* don't compare it with itself */ |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10250 |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
10251 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
|
10252 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
|
10253 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
|
10254 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
|
10255 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10256 rival = other_paths[j] + other_path_len - candidate_len; |
2574 | 10257 if (fnamecmp(maybe_unique, rival) == 0 |
10258 && (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
|
10259 return FALSE; /* match */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10260 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10261 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10262 return TRUE; /* no match found */ |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10263 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10264 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10265 /* |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
10266 * 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
|
10267 * 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
|
10268 * (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
|
10269 * directory) notations. |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10270 * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10271 * 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
|
10272 * expanding each into their equivalent path(s). |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10273 */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10274 static void |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10275 expand_path_option(curdir, gap) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10276 char_u *curdir; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10277 garray_T *gap; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10278 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10279 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
|
10280 ? p_path : curbuf->b_p_path; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10281 char_u *buf; |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10282 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
|
10283 int len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10284 |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10285 if ((buf = alloc((int)MAXPATHL)) == NULL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10286 return; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10287 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10288 while (*path_option != NUL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10289 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10290 copy_option_part(&path_option, buf, MAXPATHL, " ,"); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10291 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10292 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
|
10293 { |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10294 /* 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
|
10295 * "/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
|
10296 * "/path/file" + "./subdir" -> "/path/subdir" */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10297 if (curbuf->b_ffname == NULL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10298 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10299 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
|
10300 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
|
10301 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
|
10302 continue; |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10303 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
|
10304 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
|
10305 else |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10306 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
|
10307 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
|
10308 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
|
10309 } |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10310 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
|
10311 /* relative to current directory */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10312 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
|
10313 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
|
10314 /* 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
|
10315 continue; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10316 else if (!mch_isFullName(buf)) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10317 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10318 /* 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
|
10319 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
|
10320 if (len + (int)STRLEN(buf) + 3 > MAXPATHL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10321 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10322 STRMOVE(buf + len + 1, buf); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10323 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
|
10324 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
|
10325 simplify_filename(buf); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10326 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10327 |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10328 if (ga_grow(gap, 1) == FAIL) |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10329 break; |
4335 | 10330 |
10331 # if defined(MSWIN) || defined(MSDOS) | |
10332 /* Avoid the path ending in a backslash, it fails when a comma is | |
10333 * appended. */ | |
4342 | 10334 len = (int)STRLEN(buf); |
4335 | 10335 if (buf[len - 1] == '\\') |
10336 buf[len - 1] = '/'; | |
10337 # endif | |
10338 | |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10339 p = vim_strsave(buf); |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10340 if (p == NULL) |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10341 break; |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10342 ((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
|
10343 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10344 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10345 vim_free(buf); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10346 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10347 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10348 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10349 * 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
|
10350 * 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
|
10351 * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10352 * path: /foo/bar/baz |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10353 * fname: /foo/bar/baz/quux.txt |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10354 * returns: ^this |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10355 */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10356 static char_u * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10357 get_path_cutoff(fname, gap) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10358 char_u *fname; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10359 garray_T *gap; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10360 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10361 int i; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10362 int maxlen = 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10363 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
|
10364 char_u *cutoff = NULL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10365 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10366 for (i = 0; i < gap->ga_len; i++) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10367 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10368 int j = 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10369 |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10370 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
|
10371 # if defined(MSWIN) || defined(MSDOS) |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10372 || (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
|
10373 #endif |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10374 ) && 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
|
10375 j++; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10376 if (j > maxlen) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10377 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10378 maxlen = j; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10379 cutoff = &fname[j]; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10380 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10381 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10382 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10383 /* skip to the file or directory name */ |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10384 if (cutoff != NULL) |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10385 while (vim_ispathsep(*cutoff)) |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10386 mb_ptr_adv(cutoff); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10387 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10388 return cutoff; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10389 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10390 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10391 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10392 * 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
|
10393 * 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
|
10394 * 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
|
10395 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10396 static void |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10397 uniquefy_paths(gap, pattern) |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10398 garray_T *gap; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10399 char_u *pattern; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10400 { |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10401 int i; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10402 int len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10403 char_u **fnames = (char_u **)gap->ga_data; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10404 int sort_again = FALSE; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10405 char_u *pat; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10406 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
|
10407 char_u *curdir; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10408 regmatch_T regmatch; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10409 garray_T path_ga; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10410 char_u **in_curdir = NULL; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10411 char_u *short_name; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10412 |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10413 remove_duplicates(gap); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10414 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
|
10415 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10416 /* |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10417 * 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
|
10418 * 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
|
10419 * possible patterns? |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10420 */ |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
10421 len = (int)STRLEN(pattern); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10422 file_pattern = alloc(len + 2); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10423 if (file_pattern == NULL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10424 return; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10425 file_pattern[0] = '*'; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10426 file_pattern[1] = NUL; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10427 STRCAT(file_pattern, pattern); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10428 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
|
10429 vim_free(file_pattern); |
2387
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10430 if (pat == NULL) |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10431 return; |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10432 |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10433 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
|
10434 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
|
10435 vim_free(pat); |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10436 if (regmatch.regprog == NULL) |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10437 return; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10438 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10439 if ((curdir = alloc((int)(MAXPATHL))) == NULL) |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10440 goto theend; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10441 mch_dirname(curdir, MAXPATHL); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10442 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
|
10443 |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10444 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
|
10445 if (in_curdir == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10446 goto theend; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10447 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10448 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
|
10449 { |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10450 char_u *path = fnames[i]; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10451 int is_in_curdir; |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10452 char_u *dir_end = gettail_dir(path); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10453 char_u *pathsep_p; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10454 char_u *path_cutoff; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10455 |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
10456 len = (int)STRLEN(path); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10457 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
|
10458 && curdir[dir_end - path] == NUL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10459 if (is_in_curdir) |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10460 in_curdir[i] = vim_strsave(path); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10461 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10462 /* Shorten the filename while maintaining its uniqueness */ |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10463 path_cutoff = get_path_cutoff(path, &path_ga); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10464 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10465 /* we start at the end of the path */ |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10466 pathsep_p = path + len - 1; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10467 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10468 while (find_previous_pathsep(path, &pathsep_p)) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10469 if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10470 && is_unique(pathsep_p + 1, gap, i) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10471 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10472 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10473 sort_again = TRUE; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10474 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p)); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10475 break; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10476 } |
2538 | 10477 |
10478 if (mch_isFullName(path)) | |
10479 { | |
10480 /* | |
10481 * Last resort: shorten relative to curdir if possible. | |
10482 * 'possible' means: | |
10483 * 1. It is under the current directory. | |
10484 * 2. The result is actually shorter than the original. | |
10485 * | |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10486 * Before curdir After |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10487 * /foo/bar/file.txt /foo/bar ./file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10488 * c:\foo\bar\file.txt c:\foo\bar .\file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10489 * /file.txt / /file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10490 * c:\file.txt c:\ .\file.txt |
2538 | 10491 */ |
10492 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
|
10493 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
|
10494 #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
|
10495 /* On windows, |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10496 * 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
|
10497 * 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
|
10498 * name, hence: */ |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10499 && !vim_ispathsep(*short_name) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10500 #endif |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10501 ) |
2538 | 10502 { |
10503 STRCPY(path, "."); | |
10504 add_pathsep(path); | |
2562
19f4f5ee3d1f
Fix: strcat() on overlapping string. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2559
diff
changeset
|
10505 STRMOVE(path + STRLEN(path), short_name); |
2538 | 10506 } |
10507 } | |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10508 ui_breakcheck(); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10509 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10510 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10511 /* 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
|
10512 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
|
10513 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10514 char_u *rel_path; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10515 char_u *path = in_curdir[i]; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10516 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10517 if (path == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10518 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10519 |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10520 /* 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
|
10521 * Else reduce it to {filename} */ |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10522 short_name = shorten_fname(path, curdir); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10523 if (short_name == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10524 short_name = path; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10525 if (is_unique(short_name, gap, i)) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10526 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10527 STRCPY(fnames[i], short_name); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10528 continue; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10529 } |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10530 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10531 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2)); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10532 if (rel_path == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10533 goto theend; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10534 STRCPY(rel_path, "."); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10535 add_pathsep(rel_path); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10536 STRCAT(rel_path, short_name); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10537 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10538 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
|
10539 fnames[i] = rel_path; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10540 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
|
10541 ui_breakcheck(); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10542 } |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10543 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10544 theend: |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10545 vim_free(curdir); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10546 if (in_curdir != NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10547 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10548 for (i = 0; i < gap->ga_len; i++) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10549 vim_free(in_curdir[i]); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10550 vim_free(in_curdir); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10551 } |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10552 ga_clear_strings(&path_ga); |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
10553 vim_regfree(regmatch.regprog); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10554 |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10555 if (sort_again) |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10556 remove_duplicates(gap); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10557 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10558 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10559 /* |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10560 * 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
|
10561 * result in "gap". |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10562 * Returns the total number of matches. |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10563 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10564 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10565 expand_in_path(gap, pattern, flags) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10566 garray_T *gap; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10567 char_u *pattern; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10568 int flags; /* EW_* flags */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10569 { |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10570 char_u *curdir; |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10571 garray_T path_ga; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10572 char_u *paths = NULL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10573 |
2477
81bf2aaa979a
Fix crash in :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2476
diff
changeset
|
10574 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10575 return 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10576 mch_dirname(curdir, MAXPATHL); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10577 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10578 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
|
10579 expand_path_option(curdir, &path_ga); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10580 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
|
10581 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
|
10582 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
|
10583 |
5873 | 10584 paths = ga_concat_strings(&path_ga, ","); |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10585 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
|
10586 if (paths == NULL) |
2477
81bf2aaa979a
Fix crash in :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2476
diff
changeset
|
10587 return 0; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10588 |
5873 | 10589 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10590 vim_free(paths); |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10591 |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10592 return gap->ga_len; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10593 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10594 #endif |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10595 |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10596 #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
|
10597 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10598 * 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
|
10599 * 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
|
10600 */ |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10601 void |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10602 remove_duplicates(gap) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10603 garray_T *gap; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10604 { |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10605 int i; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10606 int j; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10607 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
|
10608 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10609 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
|
10610 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
|
10611 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
|
10612 { |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10613 vim_free(fnames[i]); |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10614 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
|
10615 fnames[j - 1] = fnames[j]; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10616 --gap->ga_len; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10617 } |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10618 } |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10619 #endif |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10620 |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10621 static int has_env_var __ARGS((char_u *p)); |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10622 |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10623 /* |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10624 * Return TRUE if "p" contains what looks like an environment variable. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10625 * Allowing for escaping. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10626 */ |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10627 static int |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10628 has_env_var(p) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10629 char_u *p; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10630 { |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10631 for ( ; *p; mb_ptr_adv(p)) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10632 { |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10633 if (*p == '\\' && p[1] != NUL) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10634 ++p; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10635 else if (vim_strchr((char_u *) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10636 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10637 "$%" |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10638 #else |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10639 "$" |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10640 #endif |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10641 , *p) != NULL) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10642 return TRUE; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10643 } |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10644 return FALSE; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10645 } |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10646 |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10647 #ifdef SPECIAL_WILDCHAR |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10648 static int has_special_wildchar __ARGS((char_u *p)); |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10649 |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10650 /* |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10651 * Return TRUE if "p" contains a special wildcard character. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10652 * Allowing for escaping. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10653 */ |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10654 static int |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10655 has_special_wildchar(p) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10656 char_u *p; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10657 { |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10658 for ( ; *p; mb_ptr_adv(p)) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10659 { |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10660 if (*p == '\\' && p[1] != NUL) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10661 ++p; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10662 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10663 return TRUE; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10664 } |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10665 return FALSE; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10666 } |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10667 #endif |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10668 |
7 | 10669 /* |
10670 * Generic wildcard expansion code. | |
10671 * | |
10672 * Characters in "pat" that should not be expanded must be preceded with a | |
10673 * backslash. E.g., "/path\ with\ spaces/my\*star*" | |
10674 * | |
10675 * Return FAIL when no single file was found. In this case "num_file" is not | |
10676 * set, and "file" may contain an error message. | |
10677 * Return OK when some files found. "num_file" is set to the number of | |
10678 * matches, "file" to the array of matches. Call FreeWild() later. | |
10679 */ | |
10680 int | |
10681 gen_expand_wildcards(num_pat, pat, num_file, file, flags) | |
10682 int num_pat; /* number of input patterns */ | |
10683 char_u **pat; /* array of input patterns */ | |
10684 int *num_file; /* resulting number of files */ | |
10685 char_u ***file; /* array of resulting files */ | |
10686 int flags; /* EW_* flags */ | |
10687 { | |
10688 int i; | |
10689 garray_T ga; | |
10690 char_u *p; | |
10691 static int recursive = FALSE; | |
10692 int add_pat; | |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10693 #if defined(FEAT_SEARCHPATH) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10694 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
|
10695 #endif |
7 | 10696 |
10697 /* | |
10698 * expand_env() is called to expand things like "~user". If this fails, | |
10699 * it calls ExpandOne(), which brings us back here. In this case, always | |
10700 * call the machine specific expansion function, if possible. Otherwise, | |
10701 * return FAIL. | |
10702 */ | |
10703 if (recursive) | |
10704 #ifdef SPECIAL_WILDCHAR | |
10705 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
10706 #else | |
10707 return FAIL; | |
10708 #endif | |
10709 | |
10710 #ifdef SPECIAL_WILDCHAR | |
10711 /* | |
10712 * If there are any special wildcard characters which we cannot handle | |
10713 * here, call machine specific function for all the expansion. This | |
10714 * avoids starting the shell for each argument separately. | |
10715 * For `=expr` do use the internal function. | |
10716 */ | |
10717 for (i = 0; i < num_pat; i++) | |
10718 { | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10719 if (has_special_wildchar(pat[i]) |
7 | 10720 # ifdef VIM_BACKTICK |
10721 && !(vim_backtick(pat[i]) && pat[i][1] == '=') | |
10722 # endif | |
10723 ) | |
10724 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
10725 } | |
10726 #endif | |
10727 | |
10728 recursive = TRUE; | |
10729 | |
10730 /* | |
10731 * The matching file names are stored in a growarray. Init it empty. | |
10732 */ | |
10733 ga_init2(&ga, (int)sizeof(char_u *), 30); | |
10734 | |
10735 for (i = 0; i < num_pat; ++i) | |
10736 { | |
10737 add_pat = -1; | |
10738 p = pat[i]; | |
10739 | |
10740 #ifdef VIM_BACKTICK | |
10741 if (vim_backtick(p)) | |
10742 add_pat = expand_backtick(&ga, p, flags); | |
10743 else | |
10744 #endif | |
10745 { | |
10746 /* | |
10747 * First expand environment variables, "~/" and "~user/". | |
10748 */ | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10749 if (has_env_var(p) || *p == '~') |
7 | 10750 { |
1408 | 10751 p = expand_env_save_opt(p, TRUE); |
7 | 10752 if (p == NULL) |
10753 p = pat[i]; | |
10754 #ifdef UNIX | |
10755 /* | |
10756 * On Unix, if expand_env() can't expand an environment | |
10757 * variable, use the shell to do that. Discard previously | |
10758 * found file names and start all over again. | |
10759 */ | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
10760 else if (has_env_var(p) || *p == '~') |
7 | 10761 { |
10762 vim_free(p); | |
1914 | 10763 ga_clear_strings(&ga); |
7 | 10764 i = mch_expand_wildcards(num_pat, pat, num_file, file, |
10765 flags); | |
10766 recursive = FALSE; | |
10767 return i; | |
10768 } | |
10769 #endif | |
10770 } | |
10771 | |
10772 /* | |
10773 * If there are wildcards: Expand file names and add each match to | |
10774 * the list. If there is no match, and EW_NOTFOUND is given, add | |
10775 * the pattern. | |
10776 * If there are no wildcards: Add the file name if it exists or | |
10777 * when EW_NOTFOUND is given. | |
10778 */ | |
10779 if (mch_has_exp_wildcard(p)) | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10780 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10781 #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
|
10782 if ((flags & EW_PATH) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10783 && !mch_isFullName(p) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10784 && !(p[0] == '.' |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10785 && (vim_ispathsep(p[1]) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10786 || (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
|
10787 ) |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10788 { |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10789 /* :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
|
10790 * 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
|
10791 recursive = FALSE; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10792 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
|
10793 recursive = TRUE; |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10794 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
|
10795 } |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10796 else |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10797 #endif |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10798 add_pat = mch_expandpath(&ga, p, flags); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10799 } |
7 | 10800 } |
10801 | |
10802 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) | |
10803 { | |
10804 char_u *t = backslash_halve_save(p); | |
10805 | |
10806 #if defined(MACOS_CLASSIC) | |
10807 slash_to_colon(t); | |
10808 #endif | |
10809 /* When EW_NOTFOUND is used, always add files and dirs. Makes | |
10810 * "vim c:/" work. */ | |
10811 if (flags & EW_NOTFOUND) | |
10812 addfile(&ga, t, flags | EW_DIR | EW_FILE); | |
10813 else if (mch_getperm(t) >= 0) | |
10814 addfile(&ga, t, flags); | |
10815 vim_free(t); | |
10816 } | |
10817 | |
2313
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
10818 #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
|
10819 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
|
10820 uniquefy_paths(&ga, p); |
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
10821 #endif |
7 | 10822 if (p != pat[i]) |
10823 vim_free(p); | |
10824 } | |
10825 | |
10826 *num_file = ga.ga_len; | |
10827 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; | |
10828 | |
10829 recursive = FALSE; | |
10830 | |
10831 return (ga.ga_data != NULL) ? OK : FAIL; | |
10832 } | |
10833 | |
10834 # ifdef VIM_BACKTICK | |
10835 | |
10836 /* | |
10837 * Return TRUE if we can expand this backtick thing here. | |
10838 */ | |
10839 static int | |
10840 vim_backtick(p) | |
10841 char_u *p; | |
10842 { | |
10843 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); | |
10844 } | |
10845 | |
10846 /* | |
10847 * Expand an item in `backticks` by executing it as a command. | |
10848 * Currently only works when pat[] starts and ends with a `. | |
10849 * Returns number of file names found. | |
10850 */ | |
10851 static int | |
10852 expand_backtick(gap, pat, flags) | |
10853 garray_T *gap; | |
10854 char_u *pat; | |
10855 int flags; /* EW_* flags */ | |
10856 { | |
10857 char_u *p; | |
10858 char_u *cmd; | |
10859 char_u *buffer; | |
10860 int cnt = 0; | |
10861 int i; | |
10862 | |
10863 /* Create the command: lop off the backticks. */ | |
10864 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); | |
10865 if (cmd == NULL) | |
10866 return 0; | |
10867 | |
10868 #ifdef FEAT_EVAL | |
10869 if (*cmd == '=') /* `={expr}`: Expand expression */ | |
714 | 10870 buffer = eval_to_string(cmd + 1, &p, TRUE); |
7 | 10871 else |
10872 #endif | |
24 | 10873 buffer = get_cmd_output(cmd, NULL, |
5808 | 10874 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
7 | 10875 vim_free(cmd); |
10876 if (buffer == NULL) | |
10877 return 0; | |
10878 | |
10879 cmd = buffer; | |
10880 while (*cmd != NUL) | |
10881 { | |
10882 cmd = skipwhite(cmd); /* skip over white space */ | |
10883 p = cmd; | |
10884 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ | |
10885 ++p; | |
10886 /* add an entry if it is not empty */ | |
10887 if (p > cmd) | |
10888 { | |
10889 i = *p; | |
10890 *p = NUL; | |
10891 addfile(gap, cmd, flags); | |
10892 *p = i; | |
10893 ++cnt; | |
10894 } | |
10895 cmd = p; | |
10896 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) | |
10897 ++cmd; | |
10898 } | |
10899 | |
10900 vim_free(buffer); | |
10901 return cnt; | |
10902 } | |
10903 # endif /* VIM_BACKTICK */ | |
10904 | |
10905 /* | |
10906 * Add a file to a file list. Accepted flags: | |
10907 * EW_DIR add directories | |
10908 * EW_FILE add files | |
716 | 10909 * EW_EXEC add executable files |
7 | 10910 * EW_NOTFOUND add even when it doesn't exist |
10911 * EW_ADDSLASH add slash after directory name | |
10912 */ | |
10913 void | |
10914 addfile(gap, f, flags) | |
10915 garray_T *gap; | |
10916 char_u *f; /* filename */ | |
10917 int flags; | |
10918 { | |
10919 char_u *p; | |
10920 int isdir; | |
10921 | |
10922 /* if the file/dir doesn't exist, may not add it */ | |
10923 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0) | |
10924 return; | |
10925 | |
10926 #ifdef FNAME_ILLEGAL | |
10927 /* if the file/dir contains illegal characters, don't add it */ | |
10928 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) | |
10929 return; | |
10930 #endif | |
10931 | |
10932 isdir = mch_isdir(f); | |
10933 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) | |
10934 return; | |
10935 | |
716 | 10936 /* If the file isn't executable, may not add it. Do accept directories. */ |
5782 | 10937 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f, NULL)) |
716 | 10938 return; |
10939 | |
7 | 10940 /* Make room for another item in the file list. */ |
10941 if (ga_grow(gap, 1) == FAIL) | |
10942 return; | |
10943 | |
10944 p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); | |
10945 if (p == NULL) | |
10946 return; | |
10947 | |
10948 STRCPY(p, f); | |
10949 #ifdef BACKSLASH_IN_FILENAME | |
10950 slash_adjust(p); | |
10951 #endif | |
10952 /* | |
10953 * Append a slash or backslash after directory names if none is present. | |
10954 */ | |
10955 #ifndef DONT_ADD_PATHSEP_TO_DIR | |
10956 if (isdir && (flags & EW_ADDSLASH)) | |
10957 add_pathsep(p); | |
10958 #endif | |
10959 ((char_u **)gap->ga_data)[gap->ga_len++] = p; | |
10960 } | |
10961 #endif /* !NO_EXPANDPATH */ | |
10962 | |
10963 #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) | |
10964 | |
10965 #ifndef SEEK_SET | |
10966 # define SEEK_SET 0 | |
10967 #endif | |
10968 #ifndef SEEK_END | |
10969 # define SEEK_END 2 | |
10970 #endif | |
10971 | |
10972 /* | |
10973 * Get the stdout of an external command. | |
5808 | 10974 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not |
10975 * NULL store the length there. | |
7 | 10976 * Returns an allocated string, or NULL for error. |
10977 */ | |
10978 char_u * | |
5808 | 10979 get_cmd_output(cmd, infile, flags, ret_len) |
7 | 10980 char_u *cmd; |
24 | 10981 char_u *infile; /* optional input file name */ |
7 | 10982 int flags; /* can be SHELL_SILENT */ |
5808 | 10983 int *ret_len; |
7 | 10984 { |
10985 char_u *tempname; | |
10986 char_u *command; | |
10987 char_u *buffer = NULL; | |
10988 int len; | |
10989 int i = 0; | |
10990 FILE *fd; | |
10991 | |
10992 if (check_restricted() || check_secure()) | |
10993 return NULL; | |
10994 | |
10995 /* get a name for the temp file */ | |
10996 if ((tempname = vim_tempname('o')) == NULL) | |
10997 { | |
10998 EMSG(_(e_notmp)); | |
10999 return NULL; | |
11000 } | |
11001 | |
11002 /* Add the redirection stuff */ | |
24 | 11003 command = make_filter_cmd(cmd, infile, tempname); |
7 | 11004 if (command == NULL) |
11005 goto done; | |
11006 | |
11007 /* | |
11008 * Call the shell to execute the command (errors are ignored). | |
11009 * Don't check timestamps here. | |
11010 */ | |
11011 ++no_check_timestamps; | |
11012 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); | |
11013 --no_check_timestamps; | |
11014 | |
11015 vim_free(command); | |
11016 | |
11017 /* | |
11018 * read the names from the file into memory | |
11019 */ | |
11020 # ifdef VMS | |
1224 | 11021 /* created temporary file is not always readable as binary */ |
7 | 11022 fd = mch_fopen((char *)tempname, "r"); |
11023 # else | |
11024 fd = mch_fopen((char *)tempname, READBIN); | |
11025 # endif | |
11026 | |
11027 if (fd == NULL) | |
11028 { | |
11029 EMSG2(_(e_notopen), tempname); | |
11030 goto done; | |
11031 } | |
11032 | |
11033 fseek(fd, 0L, SEEK_END); | |
11034 len = ftell(fd); /* get size of temp file */ | |
11035 fseek(fd, 0L, SEEK_SET); | |
11036 | |
11037 buffer = alloc(len + 1); | |
11038 if (buffer != NULL) | |
11039 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); | |
11040 fclose(fd); | |
11041 mch_remove(tempname); | |
11042 if (buffer == NULL) | |
11043 goto done; | |
11044 #ifdef VMS | |
11045 len = i; /* VMS doesn't give us what we asked for... */ | |
11046 #endif | |
11047 if (i != len) | |
11048 { | |
11049 EMSG2(_(e_notread), tempname); | |
11050 vim_free(buffer); | |
11051 buffer = NULL; | |
11052 } | |
5808 | 11053 else if (ret_len == NULL) |
5271
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
11054 { |
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
11055 /* Change NUL into SOH, otherwise the string is truncated. */ |
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
11056 for (i = 0; i < len; ++i) |
5275
3ddec3d25bd1
updated for version 7.4b.014
Bram Moolenaar <bram@vim.org>
parents:
5271
diff
changeset
|
11057 if (buffer[i] == NUL) |
3ddec3d25bd1
updated for version 7.4b.014
Bram Moolenaar <bram@vim.org>
parents:
5271
diff
changeset
|
11058 buffer[i] = 1; |
5271
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
11059 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
11060 buffer[len] = NUL; /* make sure the buffer is terminated */ |
5271
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
11061 } |
5808 | 11062 else |
11063 *ret_len = len; | |
7 | 11064 |
11065 done: | |
11066 vim_free(tempname); | |
11067 return buffer; | |
11068 } | |
11069 #endif | |
11070 | |
11071 /* | |
11072 * Free the list of files returned by expand_wildcards() or other expansion | |
11073 * functions. | |
11074 */ | |
11075 void | |
11076 FreeWild(count, files) | |
11077 int count; | |
11078 char_u **files; | |
11079 { | |
838 | 11080 if (count <= 0 || files == NULL) |
7 | 11081 return; |
11082 #if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */ | |
11083 /* | |
11084 * Is this still OK for when other functions than expand_wildcards() have | |
11085 * been used??? | |
11086 */ | |
11087 _fnexplodefree((char **)files); | |
11088 #else | |
11089 while (count--) | |
11090 vim_free(files[count]); | |
11091 vim_free(files); | |
11092 #endif | |
11093 } | |
11094 | |
11095 /* | |
2302
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2297
diff
changeset
|
11096 * Return TRUE when need to go to Insert mode because of 'insertmode'. |
7 | 11097 * Don't do this when still processing a command or a mapping. |
11098 * Don't do this when inside a ":normal" command. | |
11099 */ | |
11100 int | |
11101 goto_im() | |
11102 { | |
11103 return (p_im && stuff_empty() && typebuf_typed()); | |
11104 } | |
5867 | 11105 |
11106 /* | |
5911 | 11107 * Returns the isolated name of the shell in allocated memory: |
5867 | 11108 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f". |
11109 * - Remove any argument. E.g., "csh -f" -> "csh". | |
11110 * But don't allow a space in the path, so that this works: | |
11111 * "/usr/bin/csh --rcfile ~/.cshrc" | |
11112 * But don't do that for Windows, it's common to have a space in the path. | |
11113 */ | |
11114 char_u * | |
11115 get_isolated_shell_name() | |
11116 { | |
11117 char_u *p; | |
11118 | |
11119 #ifdef WIN3264 | |
11120 p = gettail(p_sh); | |
11121 p = vim_strnsave(p, (int)(skiptowhite(p) - p)); | |
11122 #else | |
11123 p = skiptowhite(p_sh); | |
11124 if (*p == NUL) | |
11125 { | |
11126 /* No white space, use the tail. */ | |
11127 p = vim_strsave(gettail(p_sh)); | |
11128 } | |
11129 else | |
11130 { | |
11131 char_u *p1, *p2; | |
11132 | |
11133 /* Find the last path separator before the space. */ | |
11134 p1 = p_sh; | |
11135 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2)) | |
11136 if (vim_ispathsep(*p2)) | |
11137 p1 = p2 + 1; | |
11138 p = vim_strnsave(p1, (int)(p - p1)); | |
11139 } | |
11140 #endif | |
11141 return p; | |
11142 } |