Mercurial > vim
annotate src/misc2.c @ 7545:4c922651fd78 v7.4.1073
commit https://github.com/vim/vim/commit/28fb79db6b52d1154e8dc63d227673648c2fce15
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Jan 9 22:28:33 2016 +0100
patch 7.4.1073
Problem: Alloc_id depends on numbers, may use the same one twice. It's not
clear from the number what it's for.
Solution: Use an enum. Add a function to lookup the enum value from the
name.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sat, 09 Jan 2016 22:30:04 +0100 |
parents | 30b74472ff81 |
children | 87e607fb6853 |
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 * misc2.c: Various functions. | |
12 */ | |
13 #include "vim.h" | |
14 | |
359 | 15 static char_u *username = NULL; /* cached result of mch_get_user_name() */ |
16 | |
17 static char_u *ff_expand_buffer = NULL; /* used for expanding filenames */ | |
18 | |
7 | 19 #if defined(FEAT_VIRTUALEDIT) || defined(PROTO) |
20 static int coladvance2 __ARGS((pos_T *pos, int addspaces, int finetune, colnr_T wcol)); | |
21 | |
22 /* | |
23 * Return TRUE if in the current mode we need to use virtual. | |
24 */ | |
25 int | |
26 virtual_active() | |
27 { | |
28 /* While an operator is being executed we return "virtual_op", because | |
29 * VIsual_active has already been reset, thus we can't check for "block" | |
30 * being used. */ | |
31 if (virtual_op != MAYBE) | |
32 return virtual_op; | |
33 return (ve_flags == VE_ALL | |
34 || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V) | |
35 || ((ve_flags & VE_INSERT) && (State & INSERT))); | |
36 } | |
37 | |
38 /* | |
39 * Get the screen position of the cursor. | |
40 */ | |
41 int | |
42 getviscol() | |
43 { | |
44 colnr_T x; | |
45 | |
46 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL); | |
47 return (int)x; | |
48 } | |
49 | |
50 /* | |
51 * Get the screen position of character col with a coladd in the cursor line. | |
52 */ | |
53 int | |
54 getviscol2(col, coladd) | |
55 colnr_T col; | |
56 colnr_T coladd; | |
57 { | |
58 colnr_T x; | |
59 pos_T pos; | |
60 | |
61 pos.lnum = curwin->w_cursor.lnum; | |
62 pos.col = col; | |
63 pos.coladd = coladd; | |
64 getvvcol(curwin, &pos, &x, NULL, NULL); | |
65 return (int)x; | |
66 } | |
67 | |
68 /* | |
1209 | 69 * Go to column "wcol", and add/insert white space as necessary to get the |
7 | 70 * cursor in that column. |
71 * The caller must have saved the cursor line for undo! | |
72 */ | |
73 int | |
74 coladvance_force(wcol) | |
75 colnr_T wcol; | |
76 { | |
77 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol); | |
78 | |
79 if (wcol == MAXCOL) | |
80 curwin->w_valid &= ~VALID_VIRTCOL; | |
81 else | |
82 { | |
83 /* Virtcol is valid */ | |
84 curwin->w_valid |= VALID_VIRTCOL; | |
85 curwin->w_virtcol = wcol; | |
86 } | |
87 return rc; | |
88 } | |
89 #endif | |
90 | |
91 /* | |
92 * Try to advance the Cursor to the specified screen column. | |
93 * If virtual editing: fine tune the cursor position. | |
94 * Note that all virtual positions off the end of a line should share | |
95 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)), | |
96 * beginning at coladd 0. | |
97 * | |
98 * return OK if desired column is reached, FAIL if not | |
99 */ | |
100 int | |
101 coladvance(wcol) | |
102 colnr_T wcol; | |
103 { | |
104 int rc = getvpos(&curwin->w_cursor, wcol); | |
105 | |
106 if (wcol == MAXCOL || rc == FAIL) | |
107 curwin->w_valid &= ~VALID_VIRTCOL; | |
44 | 108 else if (*ml_get_cursor() != TAB) |
7 | 109 { |
44 | 110 /* Virtcol is valid when not on a TAB */ |
7 | 111 curwin->w_valid |= VALID_VIRTCOL; |
112 curwin->w_virtcol = wcol; | |
113 } | |
114 return rc; | |
115 } | |
116 | |
117 /* | |
118 * Return in "pos" the position of the cursor advanced to screen column "wcol". | |
119 * return OK if desired column is reached, FAIL if not | |
120 */ | |
121 int | |
122 getvpos(pos, wcol) | |
123 pos_T *pos; | |
124 colnr_T wcol; | |
125 { | |
126 #ifdef FEAT_VIRTUALEDIT | |
127 return coladvance2(pos, FALSE, virtual_active(), wcol); | |
128 } | |
129 | |
130 static int | |
131 coladvance2(pos, addspaces, finetune, wcol) | |
132 pos_T *pos; | |
133 int addspaces; /* change the text to achieve our goal? */ | |
1209 | 134 int finetune; /* change char offset for the exact column */ |
7 | 135 colnr_T wcol; /* column to move to */ |
136 { | |
137 #endif | |
138 int idx; | |
139 char_u *ptr; | |
140 char_u *line; | |
141 colnr_T col = 0; | |
142 int csize = 0; | |
143 int one_more; | |
144 #ifdef FEAT_LINEBREAK | |
145 int head = 0; | |
146 #endif | |
147 | |
772 | 148 one_more = (State & INSERT) |
149 || restart_edit != NUL | |
150 || (VIsual_active && *p_sel != 'o') | |
151 #ifdef FEAT_VIRTUALEDIT | |
782 | 152 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL) |
772 | 153 #endif |
154 ; | |
1982 | 155 line = ml_get_buf(curbuf, pos->lnum, FALSE); |
7 | 156 |
157 if (wcol >= MAXCOL) | |
158 { | |
159 idx = (int)STRLEN(line) - 1 + one_more; | |
160 col = wcol; | |
161 | |
162 #ifdef FEAT_VIRTUALEDIT | |
163 if ((addspaces || finetune) && !VIsual_active) | |
164 { | |
165 curwin->w_curswant = linetabsize(line) + one_more; | |
166 if (curwin->w_curswant > 0) | |
167 --curwin->w_curswant; | |
168 } | |
169 #endif | |
170 } | |
171 else | |
172 { | |
173 #ifdef FEAT_VIRTUALEDIT | |
174 int width = W_WIDTH(curwin) - win_col_off(curwin); | |
175 | |
620 | 176 if (finetune |
7 | 177 && curwin->w_p_wrap |
178 # ifdef FEAT_VERTSPLIT | |
179 && curwin->w_width != 0 | |
180 # endif | |
181 && wcol >= (colnr_T)width) | |
182 { | |
183 csize = linetabsize(line); | |
184 if (csize > 0) | |
185 csize--; | |
186 | |
620 | 187 if (wcol / width > (colnr_T)csize / width |
188 && ((State & INSERT) == 0 || (int)wcol > csize + 1)) | |
7 | 189 { |
190 /* In case of line wrapping don't move the cursor beyond the | |
620 | 191 * right screen edge. In Insert mode allow going just beyond |
192 * the last character (like what happens when typing and | |
193 * reaching the right window edge). */ | |
7 | 194 wcol = (csize / width + 1) * width - 1; |
195 } | |
196 } | |
197 #endif | |
198 | |
199 ptr = line; | |
200 while (col <= wcol && *ptr != NUL) | |
201 { | |
202 /* Count a tab for what it's worth (if list mode not on) */ | |
203 #ifdef FEAT_LINEBREAK | |
5995 | 204 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head); |
39 | 205 mb_ptr_adv(ptr); |
7 | 206 #else |
5995 | 207 csize = lbr_chartabsize_adv(line, &ptr, col); |
7 | 208 #endif |
209 col += csize; | |
210 } | |
211 idx = (int)(ptr - line); | |
212 /* | |
213 * Handle all the special cases. The virtual_active() check | |
214 * is needed to ensure that a virtual position off the end of | |
215 * a line has the correct indexing. The one_more comparison | |
216 * replaces an explicit add of one_more later on. | |
217 */ | |
218 if (col > wcol || (!virtual_active() && one_more == 0)) | |
219 { | |
220 idx -= 1; | |
221 # ifdef FEAT_LINEBREAK | |
222 /* Don't count the chars from 'showbreak'. */ | |
223 csize -= head; | |
224 # endif | |
225 col -= csize; | |
226 } | |
227 | |
228 #ifdef FEAT_VIRTUALEDIT | |
229 if (virtual_active() | |
230 && addspaces | |
231 && ((col != wcol && col != wcol + 1) || csize > 1)) | |
232 { | |
233 /* 'virtualedit' is set: The difference between wcol and col is | |
234 * filled with spaces. */ | |
235 | |
236 if (line[idx] == NUL) | |
237 { | |
238 /* Append spaces */ | |
239 int correct = wcol - col; | |
240 char_u *newline = alloc(idx + correct + 1); | |
241 int t; | |
242 | |
243 if (newline == NULL) | |
244 return FAIL; | |
245 | |
246 for (t = 0; t < idx; ++t) | |
247 newline[t] = line[t]; | |
248 | |
249 for (t = 0; t < correct; ++t) | |
250 newline[t + idx] = ' '; | |
251 | |
252 newline[idx + correct] = NUL; | |
253 | |
254 ml_replace(pos->lnum, newline, FALSE); | |
255 changed_bytes(pos->lnum, (colnr_T)idx); | |
256 idx += correct; | |
257 col = wcol; | |
258 } | |
259 else | |
260 { | |
261 /* Break a tab */ | |
262 int linelen = (int)STRLEN(line); | |
263 int correct = wcol - col - csize + 1; /* negative!! */ | |
840 | 264 char_u *newline; |
7 | 265 int t, s = 0; |
266 int v; | |
267 | |
840 | 268 if (-correct > csize) |
269 return FAIL; | |
270 | |
271 newline = alloc(linelen + csize); | |
272 if (newline == NULL) | |
7 | 273 return FAIL; |
274 | |
275 for (t = 0; t < linelen; t++) | |
276 { | |
277 if (t != idx) | |
278 newline[s++] = line[t]; | |
279 else | |
280 for (v = 0; v < csize; v++) | |
281 newline[s++] = ' '; | |
282 } | |
283 | |
284 newline[linelen + csize - 1] = NUL; | |
285 | |
286 ml_replace(pos->lnum, newline, FALSE); | |
287 changed_bytes(pos->lnum, idx); | |
288 idx += (csize - 1 + correct); | |
289 col += correct; | |
290 } | |
291 } | |
292 #endif | |
293 } | |
294 | |
295 if (idx < 0) | |
296 pos->col = 0; | |
297 else | |
298 pos->col = idx; | |
299 | |
300 #ifdef FEAT_VIRTUALEDIT | |
301 pos->coladd = 0; | |
302 | |
303 if (finetune) | |
304 { | |
305 if (wcol == MAXCOL) | |
306 { | |
307 /* The width of the last character is used to set coladd. */ | |
308 if (!one_more) | |
309 { | |
310 colnr_T scol, ecol; | |
311 | |
312 getvcol(curwin, pos, &scol, NULL, &ecol); | |
313 pos->coladd = ecol - scol; | |
314 } | |
315 } | |
316 else | |
317 { | |
318 int b = (int)wcol - (int)col; | |
319 | |
320 /* The difference between wcol and col is used to set coladd. */ | |
321 if (b > 0 && b < (MAXCOL - 2 * W_WIDTH(curwin))) | |
322 pos->coladd = b; | |
323 | |
324 col += b; | |
325 } | |
326 } | |
327 #endif | |
328 | |
329 #ifdef FEAT_MBYTE | |
1982 | 330 /* prevent from moving onto a trail byte */ |
7 | 331 if (has_mbyte) |
2933 | 332 mb_adjustpos(curbuf, pos); |
7 | 333 #endif |
334 | |
335 if (col < wcol) | |
336 return FAIL; | |
337 return OK; | |
338 } | |
339 | |
340 /* | |
1621 | 341 * Increment the cursor position. See inc() for return values. |
7 | 342 */ |
343 int | |
344 inc_cursor() | |
345 { | |
346 return inc(&curwin->w_cursor); | |
347 } | |
348 | |
1621 | 349 /* |
350 * Increment the line pointer "lp" crossing line boundaries as necessary. | |
351 * Return 1 when going to the next line. | |
352 * Return 2 when moving forward onto a NUL at the end of the line). | |
353 * Return -1 when at the end of file. | |
354 * Return 0 otherwise. | |
355 */ | |
7 | 356 int |
357 inc(lp) | |
358 pos_T *lp; | |
359 { | |
360 char_u *p = ml_get_pos(lp); | |
361 | |
362 if (*p != NUL) /* still within line, move to next char (may be NUL) */ | |
363 { | |
364 #ifdef FEAT_MBYTE | |
365 if (has_mbyte) | |
366 { | |
474 | 367 int l = (*mb_ptr2len)(p); |
7 | 368 |
369 lp->col += l; | |
370 return ((p[l] != NUL) ? 0 : 2); | |
371 } | |
372 #endif | |
373 lp->col++; | |
374 #ifdef FEAT_VIRTUALEDIT | |
375 lp->coladd = 0; | |
376 #endif | |
377 return ((p[1] != NUL) ? 0 : 2); | |
378 } | |
379 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */ | |
380 { | |
381 lp->col = 0; | |
382 lp->lnum++; | |
383 #ifdef FEAT_VIRTUALEDIT | |
384 lp->coladd = 0; | |
385 #endif | |
386 return 1; | |
387 } | |
388 return -1; | |
389 } | |
390 | |
391 /* | |
392 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines | |
393 */ | |
394 int | |
395 incl(lp) | |
396 pos_T *lp; | |
397 { | |
398 int r; | |
399 | |
400 if ((r = inc(lp)) >= 1 && lp->col) | |
401 r = inc(lp); | |
402 return r; | |
403 } | |
404 | |
405 /* | |
406 * dec(p) | |
407 * | |
408 * Decrement the line pointer 'p' crossing line boundaries as necessary. | |
409 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise. | |
410 */ | |
411 int | |
412 dec_cursor() | |
413 { | |
414 return dec(&curwin->w_cursor); | |
415 } | |
416 | |
417 int | |
418 dec(lp) | |
419 pos_T *lp; | |
420 { | |
421 char_u *p; | |
422 | |
423 #ifdef FEAT_VIRTUALEDIT | |
424 lp->coladd = 0; | |
425 #endif | |
426 if (lp->col > 0) /* still within line */ | |
427 { | |
428 lp->col--; | |
429 #ifdef FEAT_MBYTE | |
430 if (has_mbyte) | |
431 { | |
432 p = ml_get(lp->lnum); | |
433 lp->col -= (*mb_head_off)(p, p + lp->col); | |
434 } | |
435 #endif | |
436 return 0; | |
437 } | |
438 if (lp->lnum > 1) /* there is a prior line */ | |
439 { | |
440 lp->lnum--; | |
441 p = ml_get(lp->lnum); | |
442 lp->col = (colnr_T)STRLEN(p); | |
443 #ifdef FEAT_MBYTE | |
444 if (has_mbyte) | |
445 lp->col -= (*mb_head_off)(p, p + lp->col); | |
446 #endif | |
447 return 1; | |
448 } | |
449 return -1; /* at start of file */ | |
450 } | |
451 | |
452 /* | |
453 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines | |
454 */ | |
455 int | |
456 decl(lp) | |
457 pos_T *lp; | |
458 { | |
459 int r; | |
460 | |
461 if ((r = dec(lp)) == 1 && lp->col) | |
462 r = dec(lp); | |
463 return r; | |
464 } | |
465 | |
466 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
467 * Get the line number relative to the current cursor position, i.e. the |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
468 * difference between line number and cursor position. Only look for lines that |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
469 * can be visible, folded lines don't count. |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
470 */ |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
471 linenr_T |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
472 get_cursor_rel_lnum(wp, lnum) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
473 win_T *wp; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
474 linenr_T lnum; /* line number to get the result for */ |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
475 { |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
476 linenr_T cursor = wp->w_cursor.lnum; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
477 linenr_T retval = 0; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
478 |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
479 #ifdef FEAT_FOLDING |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
480 if (hasAnyFolding(wp)) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
481 { |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
482 if (lnum > cursor) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
483 { |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
484 while (lnum > cursor) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
485 { |
5564 | 486 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL); |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
487 /* if lnum and cursor are in the same fold, |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
488 * now lnum <= cursor */ |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
489 if (lnum > cursor) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
490 retval++; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
491 lnum--; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
492 } |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
493 } |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
494 else if (lnum < cursor) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
495 { |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
496 while (lnum < cursor) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
497 { |
5564 | 498 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL); |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
499 /* if lnum and cursor are in the same fold, |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
500 * now lnum >= cursor */ |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
501 if (lnum < cursor) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
502 retval--; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
503 lnum++; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
504 } |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
505 } |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
506 /* else if (lnum == cursor) |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
507 * retval = 0; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
508 */ |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
509 } |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
510 else |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
511 #endif |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
512 retval = lnum - cursor; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
513 |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
514 return retval; |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
515 } |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
516 |
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2027
diff
changeset
|
517 /* |
7 | 518 * Make sure curwin->w_cursor.lnum is valid. |
519 */ | |
520 void | |
521 check_cursor_lnum() | |
522 { | |
523 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) | |
524 { | |
525 #ifdef FEAT_FOLDING | |
526 /* If there is a closed fold at the end of the file, put the cursor in | |
527 * its first line. Otherwise in the last line. */ | |
528 if (!hasFolding(curbuf->b_ml.ml_line_count, | |
529 &curwin->w_cursor.lnum, NULL)) | |
530 #endif | |
531 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; | |
532 } | |
533 if (curwin->w_cursor.lnum <= 0) | |
534 curwin->w_cursor.lnum = 1; | |
535 } | |
536 | |
537 /* | |
538 * Make sure curwin->w_cursor.col is valid. | |
539 */ | |
540 void | |
541 check_cursor_col() | |
542 { | |
2933 | 543 check_cursor_col_win(curwin); |
544 } | |
545 | |
546 /* | |
547 * Make sure win->w_cursor.col is valid. | |
548 */ | |
549 void | |
550 check_cursor_col_win(win) | |
551 win_T *win; | |
552 { | |
7 | 553 colnr_T len; |
554 #ifdef FEAT_VIRTUALEDIT | |
2933 | 555 colnr_T oldcol = win->w_cursor.col; |
556 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd; | |
557 #endif | |
558 | |
559 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE)); | |
7 | 560 if (len == 0) |
2933 | 561 win->w_cursor.col = 0; |
562 else if (win->w_cursor.col >= len) | |
7 | 563 { |
1488 | 564 /* Allow cursor past end-of-line when: |
565 * - in Insert mode or restarting Insert mode | |
566 * - in Visual mode and 'selection' isn't "old" | |
567 * - 'virtualedit' is set */ | |
620 | 568 if ((State & INSERT) || restart_edit |
7 | 569 || (VIsual_active && *p_sel != 'o') |
1488 | 570 #ifdef FEAT_VIRTUALEDIT |
571 || (ve_flags & VE_ONEMORE) | |
572 #endif | |
7 | 573 || virtual_active()) |
2933 | 574 win->w_cursor.col = len; |
7 | 575 else |
1099 | 576 { |
2933 | 577 win->w_cursor.col = len - 1; |
1099 | 578 #ifdef FEAT_MBYTE |
2933 | 579 /* Move the cursor to the head byte. */ |
1099 | 580 if (has_mbyte) |
2933 | 581 mb_adjustpos(win->w_buffer, &win->w_cursor); |
1099 | 582 #endif |
583 } | |
7 | 584 } |
2933 | 585 else if (win->w_cursor.col < 0) |
586 win->w_cursor.col = 0; | |
7 | 587 |
588 #ifdef FEAT_VIRTUALEDIT | |
589 /* If virtual editing is on, we can leave the cursor on the old position, | |
590 * only we must set it to virtual. But don't do it when at the end of the | |
591 * line. */ | |
592 if (oldcol == MAXCOL) | |
2933 | 593 win->w_cursor.coladd = 0; |
7 | 594 else if (ve_flags == VE_ALL) |
1841 | 595 { |
2933 | 596 if (oldcoladd > win->w_cursor.col) |
597 win->w_cursor.coladd = oldcoladd - win->w_cursor.col; | |
1841 | 598 else |
599 /* avoid weird number when there is a miscalculation or overflow */ | |
2933 | 600 win->w_cursor.coladd = 0; |
1841 | 601 } |
7 | 602 #endif |
603 } | |
604 | |
605 /* | |
606 * make sure curwin->w_cursor in on a valid character | |
607 */ | |
608 void | |
609 check_cursor() | |
610 { | |
611 check_cursor_lnum(); | |
612 check_cursor_col(); | |
613 } | |
614 | |
615 #if defined(FEAT_TEXTOBJ) || defined(PROTO) | |
616 /* | |
617 * Make sure curwin->w_cursor is not on the NUL at the end of the line. | |
618 * Allow it when in Visual mode and 'selection' is not "old". | |
619 */ | |
620 void | |
621 adjust_cursor_col() | |
622 { | |
623 if (curwin->w_cursor.col > 0 | |
624 && (!VIsual_active || *p_sel == 'o') | |
625 && gchar_cursor() == NUL) | |
626 --curwin->w_cursor.col; | |
627 } | |
628 #endif | |
629 | |
630 /* | |
631 * When curwin->w_leftcol has changed, adjust the cursor position. | |
632 * Return TRUE if the cursor was moved. | |
633 */ | |
634 int | |
635 leftcol_changed() | |
636 { | |
637 long lastcol; | |
638 colnr_T s, e; | |
639 int retval = FALSE; | |
640 | |
641 changed_cline_bef_curs(); | |
642 lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1; | |
643 validate_virtcol(); | |
644 | |
645 /* | |
646 * If the cursor is right or left of the screen, move it to last or first | |
647 * character. | |
648 */ | |
649 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso)) | |
650 { | |
651 retval = TRUE; | |
652 coladvance((colnr_T)(lastcol - p_siso)); | |
653 } | |
654 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso) | |
655 { | |
656 retval = TRUE; | |
657 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso)); | |
658 } | |
659 | |
660 /* | |
661 * If the start of the character under the cursor is not on the screen, | |
662 * advance the cursor one more char. If this fails (last char of the | |
663 * line) adjust the scrolling. | |
664 */ | |
665 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e); | |
666 if (e > (colnr_T)lastcol) | |
667 { | |
668 retval = TRUE; | |
669 coladvance(s - 1); | |
670 } | |
671 else if (s < curwin->w_leftcol) | |
672 { | |
673 retval = TRUE; | |
674 if (coladvance(e + 1) == FAIL) /* there isn't another character */ | |
675 { | |
676 curwin->w_leftcol = s; /* adjust w_leftcol instead */ | |
677 changed_cline_bef_curs(); | |
678 } | |
679 } | |
680 | |
681 if (retval) | |
682 curwin->w_set_curswant = TRUE; | |
683 redraw_later(NOT_VALID); | |
684 return retval; | |
685 } | |
686 | |
687 /********************************************************************** | |
688 * Various routines dealing with allocation and deallocation of memory. | |
689 */ | |
690 | |
691 #if defined(MEM_PROFILE) || defined(PROTO) | |
692 | |
693 # define MEM_SIZES 8200 | |
694 static long_u mem_allocs[MEM_SIZES]; | |
695 static long_u mem_frees[MEM_SIZES]; | |
696 static long_u mem_allocated; | |
697 static long_u mem_freed; | |
698 static long_u mem_peak; | |
699 static long_u num_alloc; | |
700 static long_u num_freed; | |
701 | |
702 static void mem_pre_alloc_s __ARGS((size_t *sizep)); | |
703 static void mem_pre_alloc_l __ARGS((long_u *sizep)); | |
704 static void mem_post_alloc __ARGS((void **pp, size_t size)); | |
705 static void mem_pre_free __ARGS((void **pp)); | |
706 | |
707 static void | |
708 mem_pre_alloc_s(sizep) | |
709 size_t *sizep; | |
710 { | |
711 *sizep += sizeof(size_t); | |
712 } | |
713 | |
714 static void | |
715 mem_pre_alloc_l(sizep) | |
716 long_u *sizep; | |
717 { | |
718 *sizep += sizeof(size_t); | |
719 } | |
720 | |
721 static void | |
722 mem_post_alloc(pp, size) | |
723 void **pp; | |
724 size_t size; | |
725 { | |
726 if (*pp == NULL) | |
727 return; | |
728 size -= sizeof(size_t); | |
729 *(long_u *)*pp = size; | |
730 if (size <= MEM_SIZES-1) | |
731 mem_allocs[size-1]++; | |
732 else | |
733 mem_allocs[MEM_SIZES-1]++; | |
734 mem_allocated += size; | |
735 if (mem_allocated - mem_freed > mem_peak) | |
736 mem_peak = mem_allocated - mem_freed; | |
737 num_alloc++; | |
738 *pp = (void *)((char *)*pp + sizeof(size_t)); | |
739 } | |
740 | |
741 static void | |
742 mem_pre_free(pp) | |
743 void **pp; | |
744 { | |
745 long_u size; | |
746 | |
747 *pp = (void *)((char *)*pp - sizeof(size_t)); | |
748 size = *(size_t *)*pp; | |
749 if (size <= MEM_SIZES-1) | |
750 mem_frees[size-1]++; | |
751 else | |
752 mem_frees[MEM_SIZES-1]++; | |
753 mem_freed += size; | |
754 num_freed++; | |
755 } | |
756 | |
757 /* | |
758 * called on exit via atexit() | |
759 */ | |
760 void | |
761 vim_mem_profile_dump() | |
762 { | |
763 int i, j; | |
764 | |
765 printf("\r\n"); | |
766 j = 0; | |
767 for (i = 0; i < MEM_SIZES - 1; i++) | |
768 { | |
769 if (mem_allocs[i] || mem_frees[i]) | |
770 { | |
771 if (mem_frees[i] > mem_allocs[i]) | |
772 printf("\r\n%s", _("ERROR: ")); | |
773 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]); | |
774 j++; | |
775 if (j > 3) | |
776 { | |
777 j = 0; | |
778 printf("\r\n"); | |
779 } | |
780 } | |
781 } | |
782 | |
783 i = MEM_SIZES - 1; | |
784 if (mem_allocs[i]) | |
785 { | |
786 printf("\r\n"); | |
787 if (mem_frees[i] > mem_allocs[i]) | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2522
diff
changeset
|
788 puts(_("ERROR: ")); |
7 | 789 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]); |
790 } | |
791 | |
792 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"), | |
793 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak); | |
794 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"), | |
795 num_alloc, num_freed); | |
796 } | |
797 | |
798 #endif /* MEM_PROFILE */ | |
799 | |
7513
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
800 #ifdef FEAT_EVAL |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
801 static int |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
802 alloc_does_fail() |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
803 { |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
804 if (alloc_fail_countdown == 0) |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
805 { |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
806 if (--alloc_fail_repeat <= 0) |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
807 alloc_fail_id = 0; |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
808 return TRUE; |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
809 } |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
810 --alloc_fail_countdown; |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
811 return FALSE; |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
812 } |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
813 #endif |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
814 |
7 | 815 /* |
816 * Some memory is reserved for error messages and for being able to | |
817 * call mf_release_all(), which needs some memory for mf_trans_add(). | |
818 */ | |
819 #if defined(MSDOS) && !defined(DJGPP) | |
820 # define SMALL_MEM | |
821 # define KEEP_ROOM 8192L | |
822 #else | |
823 # define KEEP_ROOM (2 * 8192L) | |
824 #endif | |
3634 | 825 #define KEEP_ROOM_KB (KEEP_ROOM / 1024L) |
7 | 826 |
827 /* | |
1576 | 828 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc(). |
7 | 829 * Use lalloc for larger blocks. |
830 */ | |
831 char_u * | |
832 alloc(size) | |
833 unsigned size; | |
834 { | |
835 return (lalloc((long_u)size, TRUE)); | |
836 } | |
837 | |
838 /* | |
7513
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
839 * alloc() with an ID for alloc_fail(). |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
840 */ |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
841 char_u * |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
842 alloc_id(size, id) |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
843 unsigned size; |
7545
4c922651fd78
commit https://github.com/vim/vim/commit/28fb79db6b52d1154e8dc63d227673648c2fce15
Christian Brabandt <cb@256bit.org>
parents:
7536
diff
changeset
|
844 alloc_id_T id UNUSED; |
7513
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
845 { |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
846 #ifdef FEAT_EVAL |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
847 if (alloc_fail_id == id && alloc_does_fail()) |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
848 return NULL; |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
849 #endif |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
850 return (lalloc((long_u)size, TRUE)); |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
851 } |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
852 |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
853 /* |
7 | 854 * Allocate memory and set all bytes to zero. |
855 */ | |
856 char_u * | |
857 alloc_clear(size) | |
858 unsigned size; | |
859 { | |
860 char_u *p; | |
861 | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2522
diff
changeset
|
862 p = lalloc((long_u)size, TRUE); |
7 | 863 if (p != NULL) |
864 (void)vim_memset(p, 0, (size_t)size); | |
865 return p; | |
866 } | |
867 | |
868 /* | |
869 * alloc() with check for maximum line length | |
870 */ | |
871 char_u * | |
872 alloc_check(size) | |
873 unsigned size; | |
874 { | |
875 #if !defined(UNIX) && !defined(__EMX__) | |
876 if (sizeof(int) == 2 && size > 0x7fff) | |
877 { | |
878 /* Don't hide this message */ | |
879 emsg_silent = 0; | |
880 EMSG(_("E340: Line is becoming too long")); | |
881 return NULL; | |
882 } | |
883 #endif | |
884 return (lalloc((long_u)size, TRUE)); | |
885 } | |
886 | |
887 /* | |
888 * Allocate memory like lalloc() and set all bytes to zero. | |
889 */ | |
890 char_u * | |
891 lalloc_clear(size, message) | |
892 long_u size; | |
893 int message; | |
894 { | |
895 char_u *p; | |
896 | |
897 p = (lalloc(size, message)); | |
898 if (p != NULL) | |
899 (void)vim_memset(p, 0, (size_t)size); | |
900 return p; | |
901 } | |
902 | |
903 /* | |
904 * Low level memory allocation function. | |
905 * This is used often, KEEP IT FAST! | |
906 */ | |
907 char_u * | |
908 lalloc(size, message) | |
909 long_u size; | |
910 int message; | |
911 { | |
912 char_u *p; /* pointer to new storage space */ | |
913 static int releasing = FALSE; /* don't do mf_release_all() recursive */ | |
914 int try_again; | |
915 #if defined(HAVE_AVAIL_MEM) && !defined(SMALL_MEM) | |
916 static long_u allocated = 0; /* allocated since last avail check */ | |
917 #endif | |
918 | |
919 /* Safety check for allocating zero bytes */ | |
920 if (size == 0) | |
921 { | |
922 /* Don't hide this message */ | |
923 emsg_silent = 0; | |
924 EMSGN(_("E341: Internal error: lalloc(%ld, )"), size); | |
925 return NULL; | |
926 } | |
927 | |
928 #ifdef MEM_PROFILE | |
929 mem_pre_alloc_l(&size); | |
930 #endif | |
931 | |
932 #if defined(MSDOS) && !defined(DJGPP) | |
933 if (size >= 0xfff0) /* in MSDOS we can't deal with >64K blocks */ | |
934 p = NULL; | |
935 else | |
936 #endif | |
937 | |
938 /* | |
939 * Loop when out of memory: Try to release some memfile blocks and | |
940 * if some blocks are released call malloc again. | |
941 */ | |
942 for (;;) | |
943 { | |
944 /* | |
945 * Handle three kind of systems: | |
946 * 1. No check for available memory: Just return. | |
947 * 2. Slow check for available memory: call mch_avail_mem() after | |
948 * allocating KEEP_ROOM amount of memory. | |
949 * 3. Strict check for available memory: call mch_avail_mem() | |
950 */ | |
951 if ((p = (char_u *)malloc((size_t)size)) != NULL) | |
952 { | |
953 #ifndef HAVE_AVAIL_MEM | |
954 /* 1. No check for available memory: Just return. */ | |
955 goto theend; | |
956 #else | |
957 # ifndef SMALL_MEM | |
958 /* 2. Slow check for available memory: call mch_avail_mem() after | |
959 * allocating (KEEP_ROOM / 2) amount of memory. */ | |
960 allocated += size; | |
961 if (allocated < KEEP_ROOM / 2) | |
962 goto theend; | |
963 allocated = 0; | |
964 # endif | |
965 /* 3. check for available memory: call mch_avail_mem() */ | |
3634 | 966 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing) |
7 | 967 { |
1737 | 968 free((char *)p); /* System is low... no go! */ |
7 | 969 p = NULL; |
970 } | |
971 else | |
972 goto theend; | |
973 #endif | |
974 } | |
975 /* | |
976 * Remember that mf_release_all() is being called to avoid an endless | |
977 * loop, because mf_release_all() may call alloc() recursively. | |
978 */ | |
979 if (releasing) | |
980 break; | |
981 releasing = TRUE; | |
448 | 982 |
983 clear_sb_text(); /* free any scrollback text */ | |
984 try_again = mf_release_all(); /* release as many blocks as possible */ | |
985 | |
7 | 986 releasing = FALSE; |
987 if (!try_again) | |
988 break; | |
989 } | |
990 | |
991 if (message && p == NULL) | |
992 do_outofmem_msg(size); | |
993 | |
994 theend: | |
995 #ifdef MEM_PROFILE | |
996 mem_post_alloc((void **)&p, (size_t)size); | |
997 #endif | |
998 return p; | |
999 } | |
1000 | |
7513
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1001 /* |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1002 * lalloc() with an ID for alloc_fail(). |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1003 */ |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1004 char_u * |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1005 lalloc_id(size, message, id) |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1006 long_u size; |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1007 int message; |
7545
4c922651fd78
commit https://github.com/vim/vim/commit/28fb79db6b52d1154e8dc63d227673648c2fce15
Christian Brabandt <cb@256bit.org>
parents:
7536
diff
changeset
|
1008 alloc_id_T id UNUSED; |
7513
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1009 { |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1010 #ifdef FEAT_EVAL |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1011 if (alloc_fail_id == id && alloc_does_fail()) |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1012 return NULL; |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1013 #endif |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1014 return (lalloc((long_u)size, message)); |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1015 } |
37e061ec063c
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1016 |
7 | 1017 #if defined(MEM_PROFILE) || defined(PROTO) |
1018 /* | |
1019 * realloc() with memory profiling. | |
1020 */ | |
1021 void * | |
1022 mem_realloc(ptr, size) | |
1023 void *ptr; | |
1024 size_t size; | |
1025 { | |
1026 void *p; | |
1027 | |
1028 mem_pre_free(&ptr); | |
1029 mem_pre_alloc_s(&size); | |
1030 | |
1031 p = realloc(ptr, size); | |
1032 | |
1033 mem_post_alloc(&p, size); | |
1034 | |
1035 return p; | |
1036 } | |
1037 #endif | |
1038 | |
1039 /* | |
1040 * Avoid repeating the error message many times (they take 1 second each). | |
1041 * Did_outofmem_msg is reset when a character is read. | |
1042 */ | |
1043 void | |
1044 do_outofmem_msg(size) | |
1045 long_u size; | |
1046 { | |
1047 if (!did_outofmem_msg) | |
1048 { | |
1049 /* Don't hide this message */ | |
1050 emsg_silent = 0; | |
3156 | 1051 |
1052 /* Must come first to avoid coming back here when printing the error | |
1053 * message fails, e.g. when setting v:errmsg. */ | |
1054 did_outofmem_msg = TRUE; | |
1055 | |
7 | 1056 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size); |
1057 } | |
1058 } | |
1059 | |
356 | 1060 #if defined(EXITFREE) || defined(PROTO) |
359 | 1061 |
1062 # if defined(FEAT_SEARCHPATH) | |
1063 static void free_findfile __ARGS((void)); | |
1064 # endif | |
1065 | |
356 | 1066 /* |
1067 * Free everything that we allocated. | |
1068 * Can be used to detect memory leaks, e.g., with ccmalloc. | |
359 | 1069 * NOTE: This is tricky! Things are freed that functions depend on. Don't be |
1070 * surprised if Vim crashes... | |
1071 * Some things can't be freed, esp. things local to a library function. | |
356 | 1072 */ |
1073 void | |
1074 free_all_mem() | |
1075 { | |
1076 buf_T *buf, *nextbuf; | |
359 | 1077 static int entered = FALSE; |
1078 | |
1079 /* When we cause a crash here it is caught and Vim tries to exit cleanly. | |
1080 * Don't try freeing everything again. */ | |
1081 if (entered) | |
1082 return; | |
1083 entered = TRUE; | |
356 | 1084 |
1446 | 1085 # ifdef FEAT_AUTOCMD |
6222 | 1086 /* Don't want to trigger autocommands from here on. */ |
1087 block_autocmds(); | |
1446 | 1088 # endif |
1089 | |
1090 # ifdef FEAT_WINDOWS | |
2363
acbb3a9ccc07
Avoid error when exiting in diff mode with EXITFREE defined.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1091 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */ |
acbb3a9ccc07
Avoid error when exiting in diff mode with EXITFREE defined.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1092 p_ea = FALSE; |
766 | 1093 if (first_tabpage->tp_next != NULL) |
1094 do_cmdline_cmd((char_u *)"tabonly!"); | |
1095 if (firstwin != lastwin) | |
1096 do_cmdline_cmd((char_u *)"only!"); | |
1446 | 1097 # endif |
673 | 1098 |
741 | 1099 # if defined(FEAT_SPELL) |
356 | 1100 /* Free all spell info. */ |
1101 spell_free_all(); | |
1102 # endif | |
1103 | |
359 | 1104 # if defined(FEAT_USR_CMDS) |
356 | 1105 /* Clear user commands (before deleting buffers). */ |
1106 ex_comclear(NULL); | |
359 | 1107 # endif |
356 | 1108 |
1109 # ifdef FEAT_MENU | |
1110 /* Clear menus. */ | |
1111 do_cmdline_cmd((char_u *)"aunmenu *"); | |
1993 | 1112 # ifdef FEAT_MULTI_LANG |
1113 do_cmdline_cmd((char_u *)"menutranslate clear"); | |
1114 # endif | |
356 | 1115 # endif |
1116 | |
359 | 1117 /* Clear mappings, abbreviations, breakpoints. */ |
1993 | 1118 do_cmdline_cmd((char_u *)"lmapclear"); |
1119 do_cmdline_cmd((char_u *)"xmapclear"); | |
356 | 1120 do_cmdline_cmd((char_u *)"mapclear"); |
1121 do_cmdline_cmd((char_u *)"mapclear!"); | |
1122 do_cmdline_cmd((char_u *)"abclear"); | |
359 | 1123 # if defined(FEAT_EVAL) |
1124 do_cmdline_cmd((char_u *)"breakdel *"); | |
1125 # endif | |
362 | 1126 # if defined(FEAT_PROFILE) |
1127 do_cmdline_cmd((char_u *)"profdel *"); | |
1128 # endif | |
1828 | 1129 # if defined(FEAT_KEYMAP) |
1130 do_cmdline_cmd((char_u *)"set keymap="); | |
1131 #endif | |
359 | 1132 |
1133 # ifdef FEAT_TITLE | |
1134 free_titles(); | |
1135 # endif | |
1136 # if defined(FEAT_SEARCHPATH) | |
1137 free_findfile(); | |
1138 # endif | |
356 | 1139 |
1140 /* Obviously named calls. */ | |
1141 # if defined(FEAT_AUTOCMD) | |
1142 free_all_autocmds(); | |
1143 # endif | |
1144 clear_termcodes(); | |
359 | 1145 free_all_options(); |
1146 free_all_marks(); | |
1147 alist_clear(&global_alist); | |
1148 free_homedir(); | |
3744 | 1149 # if defined(FEAT_CMDL_COMPL) |
1150 free_users(); | |
1151 # endif | |
359 | 1152 free_search_patterns(); |
1153 free_old_sub(); | |
1154 free_last_insert(); | |
1155 free_prev_shellcmd(); | |
1156 free_regexp_stuff(); | |
1157 free_tag_stuff(); | |
1158 free_cd_dir(); | |
1828 | 1159 # ifdef FEAT_SIGNS |
1160 free_signs(); | |
1161 # endif | |
1446 | 1162 # ifdef FEAT_EVAL |
359 | 1163 set_expr_line(NULL); |
1446 | 1164 # endif |
1165 # ifdef FEAT_DIFF | |
673 | 1166 diff_clear(curtab); |
1446 | 1167 # endif |
626 | 1168 clear_sb_text(); /* free any scrollback text */ |
359 | 1169 |
1170 /* Free some global vars. */ | |
1171 vim_free(username); | |
1422 | 1172 # ifdef FEAT_CLIPBOARD |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1173 vim_regfree(clip_exclude_prog); |
1422 | 1174 # endif |
359 | 1175 vim_free(last_cmdline); |
1446 | 1176 # ifdef FEAT_CMDHIST |
359 | 1177 vim_free(new_last_cmdline); |
1446 | 1178 # endif |
680 | 1179 set_keep_msg(NULL, 0); |
359 | 1180 vim_free(ff_expand_buffer); |
356 | 1181 |
1182 /* Clear cmdline history. */ | |
1183 p_hi = 0; | |
1446 | 1184 # ifdef FEAT_CMDHIST |
356 | 1185 init_history(); |
1446 | 1186 # endif |
356 | 1187 |
359 | 1188 #ifdef FEAT_QUICKFIX |
1446 | 1189 { |
1863 | 1190 win_T *win; |
1191 tabpage_T *tab; | |
1446 | 1192 |
1193 qf_free_all(NULL); | |
1194 /* Free all location lists */ | |
1863 | 1195 FOR_ALL_TAB_WINDOWS(tab, win) |
1446 | 1196 qf_free_all(win); |
1197 } | |
359 | 1198 #endif |
1199 | |
1200 /* Close all script inputs. */ | |
1201 close_all_scripts(); | |
1202 | |
1203 #if defined(FEAT_WINDOWS) | |
1204 /* Destroy all windows. Must come before freeing buffers. */ | |
1205 win_free_all(); | |
1206 #endif | |
1207 | |
1576 | 1208 /* Free all buffers. Reset 'autochdir' to avoid accessing things that |
1209 * were freed already. */ | |
1210 #ifdef FEAT_AUTOCHDIR | |
1211 p_acd = FALSE; | |
1212 #endif | |
356 | 1213 for (buf = firstbuf; buf != NULL; ) |
1214 { | |
1215 nextbuf = buf->b_next; | |
3365 | 1216 close_buffer(NULL, buf, DOBUF_WIPE, FALSE); |
356 | 1217 if (buf_valid(buf)) |
1218 buf = nextbuf; /* didn't work, try next one */ | |
1219 else | |
1220 buf = firstbuf; | |
1221 } | |
1222 | |
359 | 1223 #ifdef FEAT_ARABIC |
1224 free_cmdline_buf(); | |
356 | 1225 #endif |
1226 | |
1227 /* Clear registers. */ | |
1228 clear_registers(); | |
1229 ResetRedobuff(); | |
1230 ResetRedobuff(); | |
1231 | |
1071 | 1232 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) |
359 | 1233 vim_free(serverDelayedStartName); |
1234 #endif | |
1235 | |
356 | 1236 /* highlight info */ |
1237 free_highlight(); | |
1238 | |
362 | 1239 reset_last_sourcing(); |
1240 | |
766 | 1241 #ifdef FEAT_WINDOWS |
853 | 1242 free_tabpage(first_tabpage); |
1243 first_tabpage = NULL; | |
766 | 1244 #endif |
1245 | |
356 | 1246 # ifdef UNIX |
1247 /* Machine-specific free. */ | |
1248 mch_free_mem(); | |
1249 # endif | |
1250 | |
1251 /* message history */ | |
1252 for (;;) | |
1253 if (delete_first_msg() == FAIL) | |
1254 break; | |
1255 | |
1256 # ifdef FEAT_EVAL | |
1257 eval_clear(); | |
1258 # endif | |
1259 | |
359 | 1260 free_termoptions(); |
1261 | |
356 | 1262 /* screenlines (can't display anything now!) */ |
1263 free_screenlines(); | |
1264 | |
1265 #if defined(USE_XSMP) | |
1266 xsmp_close(); | |
1267 #endif | |
359 | 1268 #ifdef FEAT_GUI_GTK |
1269 gui_mch_free_all(); | |
1270 #endif | |
1271 clear_hl_tables(); | |
356 | 1272 |
1273 vim_free(IObuff); | |
1274 vim_free(NameBuff); | |
1275 } | |
1276 #endif | |
1277 | |
7 | 1278 /* |
2656 | 1279 * Copy "string" into newly allocated memory. |
7 | 1280 */ |
1281 char_u * | |
1282 vim_strsave(string) | |
1283 char_u *string; | |
1284 { | |
1285 char_u *p; | |
1286 unsigned len; | |
1287 | |
1288 len = (unsigned)STRLEN(string) + 1; | |
1289 p = alloc(len); | |
1290 if (p != NULL) | |
1291 mch_memmove(p, string, (size_t)len); | |
1292 return p; | |
1293 } | |
1294 | |
2656 | 1295 /* |
1296 * Copy up to "len" bytes of "string" into newly allocated memory and | |
1297 * terminate with a NUL. | |
1298 * The allocated memory always has size "len + 1", also when "string" is | |
1299 * shorter. | |
1300 */ | |
7 | 1301 char_u * |
1302 vim_strnsave(string, len) | |
1303 char_u *string; | |
1304 int len; | |
1305 { | |
1306 char_u *p; | |
1307 | |
1308 p = alloc((unsigned)(len + 1)); | |
1309 if (p != NULL) | |
1310 { | |
1311 STRNCPY(p, string, len); | |
1312 p[len] = NUL; | |
1313 } | |
1314 return p; | |
1315 } | |
1316 | |
1317 /* | |
1318 * Same as vim_strsave(), but any characters found in esc_chars are preceded | |
1319 * by a backslash. | |
1320 */ | |
1321 char_u * | |
1322 vim_strsave_escaped(string, esc_chars) | |
1323 char_u *string; | |
1324 char_u *esc_chars; | |
1325 { | |
16 | 1326 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE); |
7 | 1327 } |
1328 | |
1329 /* | |
1330 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape | |
1331 * characters where rem_backslash() would remove the backslash. | |
16 | 1332 * Escape the characters with "cc". |
7 | 1333 */ |
1334 char_u * | |
16 | 1335 vim_strsave_escaped_ext(string, esc_chars, cc, bsl) |
7 | 1336 char_u *string; |
1337 char_u *esc_chars; | |
16 | 1338 int cc; |
7 | 1339 int bsl; |
1340 { | |
1341 char_u *p; | |
1342 char_u *p2; | |
1343 char_u *escaped_string; | |
1344 unsigned length; | |
1345 #ifdef FEAT_MBYTE | |
1346 int l; | |
1347 #endif | |
1348 | |
1349 /* | |
1350 * First count the number of backslashes required. | |
1351 * Then allocate the memory and insert them. | |
1352 */ | |
1353 length = 1; /* count the trailing NUL */ | |
1354 for (p = string; *p; p++) | |
1355 { | |
1356 #ifdef FEAT_MBYTE | |
474 | 1357 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
7 | 1358 { |
1359 length += l; /* count a multibyte char */ | |
1360 p += l - 1; | |
1361 continue; | |
1362 } | |
1363 #endif | |
1364 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p))) | |
1365 ++length; /* count a backslash */ | |
1366 ++length; /* count an ordinary char */ | |
1367 } | |
1368 escaped_string = alloc(length); | |
1369 if (escaped_string != NULL) | |
1370 { | |
1371 p2 = escaped_string; | |
1372 for (p = string; *p; p++) | |
1373 { | |
1374 #ifdef FEAT_MBYTE | |
474 | 1375 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
7 | 1376 { |
1377 mch_memmove(p2, p, (size_t)l); | |
1378 p2 += l; | |
1379 p += l - 1; /* skip multibyte char */ | |
1380 continue; | |
1381 } | |
1382 #endif | |
1383 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p))) | |
16 | 1384 *p2++ = cc; |
7 | 1385 *p2++ = *p; |
1386 } | |
1387 *p2 = NUL; | |
1388 } | |
1389 return escaped_string; | |
1390 } | |
1391 | |
1685 | 1392 /* |
1393 * Return TRUE when 'shell' has "csh" in the tail. | |
1394 */ | |
1395 int | |
1396 csh_like_shell() | |
1397 { | |
1398 return (strstr((char *)gettail(p_sh), "csh") != NULL); | |
1399 } | |
1712 | 1400 |
985 | 1401 /* |
1402 * Escape "string" for use as a shell argument with system(). | |
1993 | 1403 * This uses single quotes, except when we know we need to use double quotes |
985 | 1404 * (MS-DOS and MS-Windows without 'shellslash' set). |
1673 | 1405 * Escape a newline, depending on the 'shell' option. |
1406 * When "do_special" is TRUE also replace "!", "%", "#" and things starting | |
1407 * with "<" like "<cfile>". | |
5690 | 1408 * When "do_newline" is FALSE do not escape newline unless it is csh shell. |
985 | 1409 * Returns the result in allocated memory, NULL if we have run out. |
1410 */ | |
1411 char_u * | |
5690 | 1412 vim_strsave_shellescape(string, do_special, do_newline) |
985 | 1413 char_u *string; |
1661 | 1414 int do_special; |
5690 | 1415 int do_newline; |
985 | 1416 { |
1417 unsigned length; | |
1418 char_u *p; | |
1419 char_u *d; | |
1420 char_u *escaped_string; | |
1661 | 1421 int l; |
1673 | 1422 int csh_like; |
1423 | |
1424 /* Only csh and similar shells expand '!' within single quotes. For sh and | |
1425 * the like we must not put a backslash before it, it will be taken | |
1426 * literally. If do_special is set the '!' will be escaped twice. | |
1427 * Csh also needs to have "\n" escaped twice when do_special is set. */ | |
1685 | 1428 csh_like = csh_like_shell(); |
985 | 1429 |
1430 /* First count the number of extra bytes required. */ | |
1072 | 1431 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */ |
985 | 1432 for (p = string; *p != NUL; mb_ptr_adv(p)) |
1433 { | |
1434 # if defined(WIN32) || defined(WIN16) || defined(DOS) | |
1435 if (!p_ssl) | |
1436 { | |
1437 if (*p == '"') | |
1438 ++length; /* " -> "" */ | |
1439 } | |
1440 else | |
1441 # endif | |
1442 if (*p == '\'') | |
1443 length += 3; /* ' => '\'' */ | |
5690 | 1444 if ((*p == '\n' && (csh_like || do_newline)) |
1445 || (*p == '!' && (csh_like || do_special))) | |
1673 | 1446 { |
1447 ++length; /* insert backslash */ | |
1448 if (csh_like && do_special) | |
1449 ++length; /* insert backslash */ | |
1450 } | |
1661 | 1451 if (do_special && find_cmdline_var(p, &l) >= 0) |
1452 { | |
1453 ++length; /* insert backslash */ | |
1454 p += l - 1; | |
1455 } | |
985 | 1456 } |
1457 | |
1458 /* Allocate memory for the result and fill it. */ | |
1459 escaped_string = alloc(length); | |
1460 if (escaped_string != NULL) | |
1461 { | |
1462 d = escaped_string; | |
1463 | |
1464 /* add opening quote */ | |
1465 # if defined(WIN32) || defined(WIN16) || defined(DOS) | |
1466 if (!p_ssl) | |
1467 *d++ = '"'; | |
1468 else | |
1469 # endif | |
1470 *d++ = '\''; | |
1471 | |
1472 for (p = string; *p != NUL; ) | |
1473 { | |
1474 # if defined(WIN32) || defined(WIN16) || defined(DOS) | |
1475 if (!p_ssl) | |
1476 { | |
1477 if (*p == '"') | |
1478 { | |
1479 *d++ = '"'; | |
1480 *d++ = '"'; | |
1481 ++p; | |
1482 continue; | |
1483 } | |
1484 } | |
1485 else | |
1486 # endif | |
1487 if (*p == '\'') | |
1488 { | |
1661 | 1489 *d++ = '\''; |
1490 *d++ = '\\'; | |
1491 *d++ = '\''; | |
1492 *d++ = '\''; | |
985 | 1493 ++p; |
1494 continue; | |
1495 } | |
5690 | 1496 if ((*p == '\n' && (csh_like || do_newline)) |
1497 || (*p == '!' && (csh_like || do_special))) | |
1673 | 1498 { |
1499 *d++ = '\\'; | |
1500 if (csh_like && do_special) | |
1501 *d++ = '\\'; | |
1502 *d++ = *p++; | |
1503 continue; | |
1504 } | |
1661 | 1505 if (do_special && find_cmdline_var(p, &l) >= 0) |
1506 { | |
1507 *d++ = '\\'; /* insert backslash */ | |
1508 while (--l >= 0) /* copy the var */ | |
1509 *d++ = *p++; | |
2009 | 1510 continue; |
1661 | 1511 } |
985 | 1512 |
1513 MB_COPY_CHAR(p, d); | |
1514 } | |
1515 | |
1516 /* add terminating quote and finish with a NUL */ | |
1517 # if defined(WIN32) || defined(WIN16) || defined(DOS) | |
1518 if (!p_ssl) | |
1519 *d++ = '"'; | |
1520 else | |
1521 # endif | |
1522 *d++ = '\''; | |
1523 *d = NUL; | |
1524 } | |
1525 | |
1526 return escaped_string; | |
1527 } | |
1528 | |
7 | 1529 /* |
1530 * Like vim_strsave(), but make all characters uppercase. | |
1531 * This uses ASCII lower-to-upper case translation, language independent. | |
1532 */ | |
1533 char_u * | |
1534 vim_strsave_up(string) | |
1535 char_u *string; | |
1536 { | |
1537 char_u *p1; | |
1538 | |
1539 p1 = vim_strsave(string); | |
1540 vim_strup(p1); | |
1541 return p1; | |
1542 } | |
1543 | |
1544 /* | |
1545 * Like vim_strnsave(), but make all characters uppercase. | |
1546 * This uses ASCII lower-to-upper case translation, language independent. | |
1547 */ | |
1548 char_u * | |
1549 vim_strnsave_up(string, len) | |
1550 char_u *string; | |
1551 int len; | |
1552 { | |
1553 char_u *p1; | |
1554 | |
1555 p1 = vim_strnsave(string, len); | |
1556 vim_strup(p1); | |
1557 return p1; | |
1558 } | |
1559 | |
1560 /* | |
1561 * ASCII lower-to-upper case translation, language independent. | |
1562 */ | |
1563 void | |
1564 vim_strup(p) | |
1565 char_u *p; | |
1566 { | |
1567 char_u *p2; | |
1568 int c; | |
1569 | |
1570 if (p != NULL) | |
1571 { | |
1572 p2 = p; | |
1573 while ((c = *p2) != NUL) | |
1574 #ifdef EBCDIC | |
1575 *p2++ = isalpha(c) ? toupper(c) : c; | |
1576 #else | |
1577 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20); | |
1578 #endif | |
1579 } | |
1580 } | |
1581 | |
741 | 1582 #if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO) |
323 | 1583 /* |
1584 * Make string "s" all upper-case and return it in allocated memory. | |
1585 * Handles multi-byte characters as well as possible. | |
1586 * Returns NULL when out of memory. | |
1587 */ | |
1588 char_u * | |
1589 strup_save(orig) | |
1590 char_u *orig; | |
1591 { | |
1592 char_u *p; | |
1593 char_u *res; | |
1594 | |
1595 res = p = vim_strsave(orig); | |
1596 | |
1597 if (res != NULL) | |
1598 while (*p != NUL) | |
1599 { | |
1600 # ifdef FEAT_MBYTE | |
1601 int l; | |
1602 | |
1603 if (enc_utf8) | |
1604 { | |
1605 int c, uc; | |
3263 | 1606 int newl; |
323 | 1607 char_u *s; |
1608 | |
1609 c = utf_ptr2char(p); | |
1610 uc = utf_toupper(c); | |
1611 | |
1612 /* Reallocate string when byte count changes. This is rare, | |
1613 * thus it's OK to do another malloc()/free(). */ | |
474 | 1614 l = utf_ptr2len(p); |
3263 | 1615 newl = utf_char2len(uc); |
1616 if (newl != l) | |
323 | 1617 { |
3263 | 1618 s = alloc((unsigned)STRLEN(res) + 1 + newl - l); |
323 | 1619 if (s == NULL) |
1620 break; | |
1621 mch_memmove(s, res, p - res); | |
3263 | 1622 STRCPY(s + (p - res) + newl, p + l); |
323 | 1623 p = s + (p - res); |
1624 vim_free(res); | |
1625 res = s; | |
1626 } | |
1627 | |
1628 utf_char2bytes(uc, p); | |
3263 | 1629 p += newl; |
323 | 1630 } |
474 | 1631 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
323 | 1632 p += l; /* skip multi-byte character */ |
1633 else | |
1634 # endif | |
1635 { | |
1636 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */ | |
1637 p++; | |
1638 } | |
1639 } | |
1640 | |
1641 return res; | |
1642 } | |
1643 #endif | |
1644 | |
7 | 1645 /* |
1646 * delete spaces at the end of a string | |
1647 */ | |
1648 void | |
1649 del_trailing_spaces(ptr) | |
1650 char_u *ptr; | |
1651 { | |
1652 char_u *q; | |
1653 | |
1654 q = ptr + STRLEN(ptr); | |
1655 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V) | |
1656 *q = NUL; | |
1657 } | |
1658 | |
1659 /* | |
323 | 1660 * Like strncpy(), but always terminate the result with one NUL. |
378 | 1661 * "to" must be "len + 1" long! |
7 | 1662 */ |
1663 void | |
1664 vim_strncpy(to, from, len) | |
323 | 1665 char_u *to; |
1666 char_u *from; | |
418 | 1667 size_t len; |
7 | 1668 { |
323 | 1669 STRNCPY(to, from, len); |
1670 to[len] = NUL; | |
7 | 1671 } |
1672 | |
1673 /* | |
2768 | 1674 * Like strcat(), but make sure the result fits in "tosize" bytes and is |
1675 * always NUL terminated. | |
1676 */ | |
1677 void | |
1678 vim_strcat(to, from, tosize) | |
1679 char_u *to; | |
1680 char_u *from; | |
1681 size_t tosize; | |
1682 { | |
1683 size_t tolen = STRLEN(to); | |
1684 size_t fromlen = STRLEN(from); | |
1685 | |
1686 if (tolen + fromlen + 1 > tosize) | |
1687 { | |
1688 mch_memmove(to + tolen, from, tosize - tolen - 1); | |
1689 to[tosize - 1] = NUL; | |
1690 } | |
1691 else | |
1692 STRCPY(to + tolen, from); | |
1693 } | |
1694 | |
1695 /* | |
7 | 1696 * Isolate one part of a string option where parts are separated with |
1697 * "sep_chars". | |
459 | 1698 * The part is copied into "buf[maxlen]". |
7 | 1699 * "*option" is advanced to the next part. |
1700 * The length is returned. | |
1701 */ | |
1702 int | |
1703 copy_option_part(option, buf, maxlen, sep_chars) | |
1704 char_u **option; | |
1705 char_u *buf; | |
1706 int maxlen; | |
1707 char *sep_chars; | |
1708 { | |
1709 int len = 0; | |
1710 char_u *p = *option; | |
1711 | |
1712 /* skip '.' at start of option part, for 'suffixes' */ | |
1713 if (*p == '.') | |
1714 buf[len++] = *p++; | |
1715 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL) | |
1716 { | |
1717 /* | |
1718 * Skip backslash before a separator character and space. | |
1719 */ | |
1720 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL) | |
1721 ++p; | |
1722 if (len < maxlen - 1) | |
1723 buf[len++] = *p; | |
1724 ++p; | |
1725 } | |
1726 buf[len] = NUL; | |
1727 | |
1728 if (*p != NUL && *p != ',') /* skip non-standard separator */ | |
1729 ++p; | |
1730 p = skip_to_option_part(p); /* p points to next file name */ | |
1731 | |
1732 *option = p; | |
1733 return len; | |
1734 } | |
1735 | |
1736 /* | |
625 | 1737 * Replacement for free() that ignores NULL pointers. |
1738 * Also skip free() when exiting for sure, this helps when we caught a deadly | |
1739 * signal that was caused by a crash in free(). | |
7 | 1740 */ |
1741 void | |
1742 vim_free(x) | |
1743 void *x; | |
1744 { | |
625 | 1745 if (x != NULL && !really_exiting) |
7 | 1746 { |
1747 #ifdef MEM_PROFILE | |
1748 mem_pre_free(&x); | |
1749 #endif | |
1750 free(x); | |
1751 } | |
1752 } | |
1753 | |
1754 #ifndef HAVE_MEMSET | |
1755 void * | |
1756 vim_memset(ptr, c, size) | |
1757 void *ptr; | |
1758 int c; | |
1759 size_t size; | |
1760 { | |
1761 char *p = ptr; | |
1762 | |
1763 while (size-- > 0) | |
1764 *p++ = c; | |
1765 return ptr; | |
1766 } | |
1767 #endif | |
1768 | |
1769 #ifdef VIM_MEMCMP | |
1770 /* | |
1771 * Return zero when "b1" and "b2" are the same for "len" bytes. | |
1772 * Return non-zero otherwise. | |
1773 */ | |
1774 int | |
1775 vim_memcmp(b1, b2, len) | |
1776 void *b1; | |
1777 void *b2; | |
1778 size_t len; | |
1779 { | |
1780 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2; | |
1781 | |
1782 for ( ; len > 0; --len) | |
1783 { | |
1784 if (*p1 != *p2) | |
1785 return 1; | |
1786 ++p1; | |
1787 ++p2; | |
1788 } | |
1789 return 0; | |
1790 } | |
1791 #endif | |
1792 | |
1793 #ifdef VIM_MEMMOVE | |
1794 /* | |
1795 * Version of memmove() that handles overlapping source and destination. | |
1796 * For systems that don't have a function that is guaranteed to do that (SYSV). | |
1797 */ | |
1798 void | |
1799 mch_memmove(dst_arg, src_arg, len) | |
1800 void *src_arg, *dst_arg; | |
1801 size_t len; | |
1802 { | |
1803 /* | |
1804 * A void doesn't have a size, we use char pointers. | |
1805 */ | |
1806 char *dst = dst_arg, *src = src_arg; | |
1807 | |
1808 /* overlap, copy backwards */ | |
1809 if (dst > src && dst < src + len) | |
1810 { | |
1811 src += len; | |
1812 dst += len; | |
1813 while (len-- > 0) | |
1814 *--dst = *--src; | |
1815 } | |
1816 else /* copy forwards */ | |
1817 while (len-- > 0) | |
1818 *dst++ = *src++; | |
1819 } | |
1820 #endif | |
1821 | |
1822 #if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO) | |
1823 /* | |
1824 * Compare two strings, ignoring case, using current locale. | |
1825 * Doesn't work for multi-byte characters. | |
1826 * return 0 for match, < 0 for smaller, > 0 for bigger | |
1827 */ | |
1828 int | |
1829 vim_stricmp(s1, s2) | |
1830 char *s1; | |
1831 char *s2; | |
1832 { | |
1833 int i; | |
1834 | |
1835 for (;;) | |
1836 { | |
1837 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); | |
1838 if (i != 0) | |
1839 return i; /* this character different */ | |
1840 if (*s1 == NUL) | |
1841 break; /* strings match until NUL */ | |
1842 ++s1; | |
1843 ++s2; | |
1844 } | |
1845 return 0; /* strings match */ | |
1846 } | |
1847 #endif | |
1848 | |
1849 #if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO) | |
1850 /* | |
1851 * Compare two strings, for length "len", ignoring case, using current locale. | |
1852 * Doesn't work for multi-byte characters. | |
1853 * return 0 for match, < 0 for smaller, > 0 for bigger | |
1854 */ | |
1855 int | |
1856 vim_strnicmp(s1, s2, len) | |
1857 char *s1; | |
1858 char *s2; | |
1859 size_t len; | |
1860 { | |
1861 int i; | |
1862 | |
1863 while (len > 0) | |
1864 { | |
1865 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); | |
1866 if (i != 0) | |
1867 return i; /* this character different */ | |
1868 if (*s1 == NUL) | |
1869 break; /* strings match until NUL */ | |
1870 ++s1; | |
1871 ++s2; | |
1872 --len; | |
1873 } | |
1874 return 0; /* strings match */ | |
1875 } | |
1876 #endif | |
1877 | |
1878 /* | |
1879 * Version of strchr() and strrchr() that handle unsigned char strings | |
170 | 1880 * with characters from 128 to 255 correctly. It also doesn't return a |
1881 * pointer to the NUL at the end of the string. | |
7 | 1882 */ |
1883 char_u * | |
1884 vim_strchr(string, c) | |
1885 char_u *string; | |
1886 int c; | |
1887 { | |
1888 char_u *p; | |
1889 int b; | |
1890 | |
1891 p = string; | |
1892 #ifdef FEAT_MBYTE | |
1893 if (enc_utf8 && c >= 0x80) | |
1894 { | |
1895 while (*p != NUL) | |
1896 { | |
6765 | 1897 int l = (*mb_ptr2len)(p); |
1898 | |
1899 /* Avoid matching an illegal byte here. */ | |
1900 if (utf_ptr2char(p) == c && l > 1) | |
7 | 1901 return p; |
6765 | 1902 p += l; |
7 | 1903 } |
1904 return NULL; | |
1905 } | |
1906 if (enc_dbcs != 0 && c > 255) | |
1907 { | |
1908 int n2 = c & 0xff; | |
1909 | |
1910 c = ((unsigned)c >> 8) & 0xff; | |
1911 while ((b = *p) != NUL) | |
1912 { | |
1913 if (b == c && p[1] == n2) | |
1914 return p; | |
474 | 1915 p += (*mb_ptr2len)(p); |
7 | 1916 } |
1917 return NULL; | |
1918 } | |
1919 if (has_mbyte) | |
1920 { | |
1921 while ((b = *p) != NUL) | |
1922 { | |
1923 if (b == c) | |
1924 return p; | |
474 | 1925 p += (*mb_ptr2len)(p); |
7 | 1926 } |
1927 return NULL; | |
1928 } | |
1929 #endif | |
1930 while ((b = *p) != NUL) | |
1931 { | |
1932 if (b == c) | |
1933 return p; | |
1934 ++p; | |
1935 } | |
1936 return NULL; | |
1937 } | |
1938 | |
1939 /* | |
170 | 1940 * Version of strchr() that only works for bytes and handles unsigned char |
1941 * strings with characters above 128 correctly. It also doesn't return a | |
1942 * pointer to the NUL at the end of the string. | |
1943 */ | |
1944 char_u * | |
1945 vim_strbyte(string, c) | |
1946 char_u *string; | |
1947 int c; | |
1948 { | |
1949 char_u *p = string; | |
1950 | |
1951 while (*p != NUL) | |
1952 { | |
1953 if (*p == c) | |
1954 return p; | |
1955 ++p; | |
1956 } | |
1957 return NULL; | |
1958 } | |
1959 | |
1960 /* | |
7 | 1961 * Search for last occurrence of "c" in "string". |
500 | 1962 * Return NULL if not found. |
170 | 1963 * Does not handle multi-byte char for "c"! |
7 | 1964 */ |
1965 char_u * | |
1966 vim_strrchr(string, c) | |
1967 char_u *string; | |
1968 int c; | |
1969 { | |
1970 char_u *retval = NULL; | |
170 | 1971 char_u *p = string; |
1972 | |
1973 while (*p) | |
7 | 1974 { |
170 | 1975 if (*p == c) |
1976 retval = p; | |
1977 mb_ptr_adv(p); | |
7 | 1978 } |
1979 return retval; | |
1980 } | |
1981 | |
1982 /* | |
1983 * Vim's version of strpbrk(), in case it's missing. | |
1984 * Don't generate a prototype for this, causes problems when it's not used. | |
1985 */ | |
1986 #ifndef PROTO | |
1987 # ifndef HAVE_STRPBRK | |
1988 # ifdef vim_strpbrk | |
1989 # undef vim_strpbrk | |
1990 # endif | |
1991 char_u * | |
1992 vim_strpbrk(s, charset) | |
1993 char_u *s; | |
1994 char_u *charset; | |
1995 { | |
1996 while (*s) | |
1997 { | |
1998 if (vim_strchr(charset, *s) != NULL) | |
1999 return s; | |
39 | 2000 mb_ptr_adv(s); |
7 | 2001 } |
2002 return NULL; | |
2003 } | |
2004 # endif | |
2005 #endif | |
2006 | |
2007 /* | |
2008 * Vim has its own isspace() function, because on some machines isspace() | |
2009 * can't handle characters above 128. | |
2010 */ | |
2011 int | |
2012 vim_isspace(x) | |
2013 int x; | |
2014 { | |
2015 return ((x >= 9 && x <= 13) || x == ' '); | |
2016 } | |
2017 | |
2018 /************************************************************************ | |
119 | 2019 * Functions for handling growing arrays. |
7 | 2020 */ |
2021 | |
2022 /* | |
2023 * Clear an allocated growing array. | |
2024 */ | |
2025 void | |
2026 ga_clear(gap) | |
2027 garray_T *gap; | |
2028 { | |
2029 vim_free(gap->ga_data); | |
2030 ga_init(gap); | |
2031 } | |
2032 | |
2033 /* | |
2034 * Clear a growing array that contains a list of strings. | |
2035 */ | |
2036 void | |
2037 ga_clear_strings(gap) | |
2038 garray_T *gap; | |
2039 { | |
2040 int i; | |
2041 | |
2042 for (i = 0; i < gap->ga_len; ++i) | |
2043 vim_free(((char_u **)(gap->ga_data))[i]); | |
2044 ga_clear(gap); | |
2045 } | |
2046 | |
2047 /* | |
2048 * Initialize a growing array. Don't forget to set ga_itemsize and | |
2049 * ga_growsize! Or use ga_init2(). | |
2050 */ | |
2051 void | |
2052 ga_init(gap) | |
2053 garray_T *gap; | |
2054 { | |
2055 gap->ga_data = NULL; | |
41 | 2056 gap->ga_maxlen = 0; |
7 | 2057 gap->ga_len = 0; |
2058 } | |
2059 | |
2060 void | |
2061 ga_init2(gap, itemsize, growsize) | |
2062 garray_T *gap; | |
2063 int itemsize; | |
2064 int growsize; | |
2065 { | |
2066 ga_init(gap); | |
2067 gap->ga_itemsize = itemsize; | |
2068 gap->ga_growsize = growsize; | |
2069 } | |
2070 | |
2071 /* | |
2072 * Make room in growing array "gap" for at least "n" items. | |
2073 * Return FAIL for failure, OK otherwise. | |
2074 */ | |
2075 int | |
2076 ga_grow(gap, n) | |
2077 garray_T *gap; | |
2078 int n; | |
2079 { | |
3376 | 2080 size_t old_len; |
2081 size_t new_len; | |
7 | 2082 char_u *pp; |
2083 | |
41 | 2084 if (gap->ga_maxlen - gap->ga_len < n) |
7 | 2085 { |
2086 if (n < gap->ga_growsize) | |
2087 n = gap->ga_growsize; | |
3376 | 2088 new_len = gap->ga_itemsize * (gap->ga_len + n); |
2089 pp = (gap->ga_data == NULL) | |
3386 | 2090 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len); |
7 | 2091 if (pp == NULL) |
2092 return FAIL; | |
3376 | 2093 old_len = gap->ga_itemsize * gap->ga_maxlen; |
2094 vim_memset(pp + old_len, 0, new_len - old_len); | |
41 | 2095 gap->ga_maxlen = gap->ga_len + n; |
7 | 2096 gap->ga_data = pp; |
2097 } | |
2098 return OK; | |
2099 } | |
2100 | |
2101 /* | |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2102 * For a growing array that contains a list of strings: concatenate all the |
5873 | 2103 * strings with a separating "sep". |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2104 * Returns NULL when out of memory. |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2105 */ |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2106 char_u * |
5873 | 2107 ga_concat_strings(gap, sep) |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2108 garray_T *gap; |
5873 | 2109 char *sep; |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2110 { |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2111 int i; |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2112 int len = 0; |
5873 | 2113 int sep_len = (int)STRLEN(sep); |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2114 char_u *s; |
5873 | 2115 char_u *p; |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2116 |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2117 for (i = 0; i < gap->ga_len; ++i) |
5873 | 2118 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len; |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2119 |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2120 s = alloc(len + 1); |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2121 if (s != NULL) |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2122 { |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2123 *s = NUL; |
5873 | 2124 p = s; |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2125 for (i = 0; i < gap->ga_len; ++i) |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2126 { |
5873 | 2127 if (p != s) |
2128 { | |
2129 STRCPY(p, sep); | |
2130 p += sep_len; | |
2131 } | |
2132 STRCPY(p, ((char_u **)(gap->ga_data))[i]); | |
2133 p += STRLEN(p); | |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2134 } |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2135 } |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2136 return s; |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2137 } |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2138 |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
2139 /* |
7 | 2140 * Concatenate a string to a growarray which contains characters. |
7277
6600871bb38c
commit https://github.com/vim/vim/commit/43345546ae63710441f066648b8485fb545b3801
Christian Brabandt <cb@256bit.org>
parents:
7214
diff
changeset
|
2141 * When "s" is NULL does not do anything. |
7 | 2142 * Note: Does NOT copy the NUL at the end! |
2143 */ | |
2144 void | |
2145 ga_concat(gap, s) | |
2146 garray_T *gap; | |
2147 char_u *s; | |
2148 { | |
7277
6600871bb38c
commit https://github.com/vim/vim/commit/43345546ae63710441f066648b8485fb545b3801
Christian Brabandt <cb@256bit.org>
parents:
7214
diff
changeset
|
2149 int len; |
6600871bb38c
commit https://github.com/vim/vim/commit/43345546ae63710441f066648b8485fb545b3801
Christian Brabandt <cb@256bit.org>
parents:
7214
diff
changeset
|
2150 |
6600871bb38c
commit https://github.com/vim/vim/commit/43345546ae63710441f066648b8485fb545b3801
Christian Brabandt <cb@256bit.org>
parents:
7214
diff
changeset
|
2151 if (s == NULL) |
6600871bb38c
commit https://github.com/vim/vim/commit/43345546ae63710441f066648b8485fb545b3801
Christian Brabandt <cb@256bit.org>
parents:
7214
diff
changeset
|
2152 return; |
6600871bb38c
commit https://github.com/vim/vim/commit/43345546ae63710441f066648b8485fb545b3801
Christian Brabandt <cb@256bit.org>
parents:
7214
diff
changeset
|
2153 len = (int)STRLEN(s); |
7 | 2154 if (ga_grow(gap, len) == OK) |
2155 { | |
2156 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len); | |
2157 gap->ga_len += len; | |
2158 } | |
2159 } | |
2160 | |
2161 /* | |
2162 * Append one byte to a growarray which contains bytes. | |
2163 */ | |
2164 void | |
2165 ga_append(gap, c) | |
2166 garray_T *gap; | |
2167 int c; | |
2168 { | |
2169 if (ga_grow(gap, 1) == OK) | |
2170 { | |
2171 *((char *)gap->ga_data + gap->ga_len) = c; | |
2172 ++gap->ga_len; | |
2173 } | |
2174 } | |
2175 | |
5995 | 2176 #if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \ |
2177 || defined(PROTO) | |
2935 | 2178 /* |
2179 * Append the text in "gap" below the cursor line and clear "gap". | |
2180 */ | |
2181 void | |
2182 append_ga_line(gap) | |
2183 garray_T *gap; | |
2184 { | |
2185 /* Remove trailing CR. */ | |
2186 if (gap->ga_len > 0 | |
2187 && !curbuf->b_p_bin | |
2188 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR) | |
2189 --gap->ga_len; | |
2190 ga_append(gap, NUL); | |
2191 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE); | |
2192 gap->ga_len = 0; | |
2193 } | |
2194 #endif | |
2195 | |
7 | 2196 /************************************************************************ |
2197 * functions that use lookup tables for various things, generally to do with | |
2198 * special key codes. | |
2199 */ | |
2200 | |
2201 /* | |
2202 * Some useful tables. | |
2203 */ | |
2204 | |
2205 static struct modmasktable | |
2206 { | |
2207 short mod_mask; /* Bit-mask for particular key modifier */ | |
2208 short mod_flag; /* Bit(s) for particular key modifier */ | |
2209 char_u name; /* Single letter name of modifier */ | |
2210 } mod_mask_table[] = | |
2211 { | |
2212 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'}, | |
179 | 2213 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'}, |
7 | 2214 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'}, |
2215 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'}, | |
2216 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'}, | |
2217 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'}, | |
2218 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'}, | |
2219 #ifdef MACOS | |
2220 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'}, | |
2221 #endif | |
2222 /* 'A' must be the last one */ | |
2223 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'}, | |
2224 {0, 0, NUL} | |
2225 }; | |
2226 | |
2227 /* | |
2228 * Shifted key terminal codes and their unshifted equivalent. | |
1209 | 2229 * Don't add mouse codes here, they are handled separately! |
7 | 2230 */ |
2231 #define MOD_KEYS_ENTRY_SIZE 5 | |
2232 | |
2233 static char_u modifier_keys_table[] = | |
2234 { | |
2235 /* mod mask with modifier without modifier */ | |
2236 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */ | |
2237 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */ | |
2238 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */ | |
2239 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */ | |
2240 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */ | |
2241 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */ | |
2242 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */ | |
2243 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */ | |
2244 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */ | |
2245 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */ | |
2246 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */ | |
2247 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */ | |
2248 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */ | |
2249 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */ | |
2250 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */ | |
2251 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */ | |
2252 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */ | |
2253 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */ | |
2254 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */ | |
2255 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */ | |
2256 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */ | |
2257 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */ | |
2258 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */ | |
2259 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */ | |
2260 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */ | |
2261 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */ | |
2262 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */ | |
2263 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */ | |
2264 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */ | |
2265 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */ | |
2266 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */ | |
2267 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */ | |
2268 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */ | |
2269 | |
2270 /* vt100 F1 */ | |
2271 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1, | |
2272 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2, | |
2273 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3, | |
2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4, | |
2275 | |
2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */ | |
2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2', | |
2278 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3', | |
2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4', | |
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5', | |
2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6', | |
2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7', | |
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8', | |
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9', | |
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */ | |
2286 | |
2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1', | |
2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2', | |
2289 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3', | |
2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4', | |
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5', | |
2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6', | |
2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7', | |
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8', | |
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9', | |
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A', | |
2297 | |
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B', | |
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C', | |
2300 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D', | |
2301 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E', | |
2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F', | |
2303 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G', | |
2304 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H', | |
2305 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I', | |
2306 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J', | |
2307 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K', | |
2308 | |
2309 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L', | |
2310 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M', | |
2311 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N', | |
2312 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O', | |
2313 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P', | |
2314 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q', | |
2315 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R', | |
2316 | |
2317 /* TAB pseudo code*/ | |
2318 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB, | |
2319 | |
2320 NUL | |
2321 }; | |
2322 | |
2323 static struct key_name_entry | |
2324 { | |
2325 int key; /* Special key code or ascii value */ | |
2326 char_u *name; /* Name of key */ | |
2327 } key_names_table[] = | |
2328 { | |
2329 {' ', (char_u *)"Space"}, | |
2330 {TAB, (char_u *)"Tab"}, | |
2331 {K_TAB, (char_u *)"Tab"}, | |
2332 {NL, (char_u *)"NL"}, | |
2333 {NL, (char_u *)"NewLine"}, /* Alternative name */ | |
2334 {NL, (char_u *)"LineFeed"}, /* Alternative name */ | |
2335 {NL, (char_u *)"LF"}, /* Alternative name */ | |
2336 {CAR, (char_u *)"CR"}, | |
2337 {CAR, (char_u *)"Return"}, /* Alternative name */ | |
2338 {CAR, (char_u *)"Enter"}, /* Alternative name */ | |
2339 {K_BS, (char_u *)"BS"}, | |
2340 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */ | |
2341 {ESC, (char_u *)"Esc"}, | |
2342 {CSI, (char_u *)"CSI"}, | |
2343 {K_CSI, (char_u *)"xCSI"}, | |
2344 {'|', (char_u *)"Bar"}, | |
2345 {'\\', (char_u *)"Bslash"}, | |
2346 {K_DEL, (char_u *)"Del"}, | |
2347 {K_DEL, (char_u *)"Delete"}, /* Alternative name */ | |
2348 {K_KDEL, (char_u *)"kDel"}, | |
2349 {K_UP, (char_u *)"Up"}, | |
2350 {K_DOWN, (char_u *)"Down"}, | |
2351 {K_LEFT, (char_u *)"Left"}, | |
2352 {K_RIGHT, (char_u *)"Right"}, | |
180 | 2353 {K_XUP, (char_u *)"xUp"}, |
2354 {K_XDOWN, (char_u *)"xDown"}, | |
2355 {K_XLEFT, (char_u *)"xLeft"}, | |
2356 {K_XRIGHT, (char_u *)"xRight"}, | |
7 | 2357 |
2358 {K_F1, (char_u *)"F1"}, | |
2359 {K_F2, (char_u *)"F2"}, | |
2360 {K_F3, (char_u *)"F3"}, | |
2361 {K_F4, (char_u *)"F4"}, | |
2362 {K_F5, (char_u *)"F5"}, | |
2363 {K_F6, (char_u *)"F6"}, | |
2364 {K_F7, (char_u *)"F7"}, | |
2365 {K_F8, (char_u *)"F8"}, | |
2366 {K_F9, (char_u *)"F9"}, | |
2367 {K_F10, (char_u *)"F10"}, | |
2368 | |
2369 {K_F11, (char_u *)"F11"}, | |
2370 {K_F12, (char_u *)"F12"}, | |
2371 {K_F13, (char_u *)"F13"}, | |
2372 {K_F14, (char_u *)"F14"}, | |
2373 {K_F15, (char_u *)"F15"}, | |
2374 {K_F16, (char_u *)"F16"}, | |
2375 {K_F17, (char_u *)"F17"}, | |
2376 {K_F18, (char_u *)"F18"}, | |
2377 {K_F19, (char_u *)"F19"}, | |
2378 {K_F20, (char_u *)"F20"}, | |
2379 | |
2380 {K_F21, (char_u *)"F21"}, | |
2381 {K_F22, (char_u *)"F22"}, | |
2382 {K_F23, (char_u *)"F23"}, | |
2383 {K_F24, (char_u *)"F24"}, | |
2384 {K_F25, (char_u *)"F25"}, | |
2385 {K_F26, (char_u *)"F26"}, | |
2386 {K_F27, (char_u *)"F27"}, | |
2387 {K_F28, (char_u *)"F28"}, | |
2388 {K_F29, (char_u *)"F29"}, | |
2389 {K_F30, (char_u *)"F30"}, | |
2390 | |
2391 {K_F31, (char_u *)"F31"}, | |
2392 {K_F32, (char_u *)"F32"}, | |
2393 {K_F33, (char_u *)"F33"}, | |
2394 {K_F34, (char_u *)"F34"}, | |
2395 {K_F35, (char_u *)"F35"}, | |
2396 {K_F36, (char_u *)"F36"}, | |
2397 {K_F37, (char_u *)"F37"}, | |
2398 | |
2399 {K_XF1, (char_u *)"xF1"}, | |
2400 {K_XF2, (char_u *)"xF2"}, | |
2401 {K_XF3, (char_u *)"xF3"}, | |
2402 {K_XF4, (char_u *)"xF4"}, | |
2403 | |
2404 {K_HELP, (char_u *)"Help"}, | |
2405 {K_UNDO, (char_u *)"Undo"}, | |
2406 {K_INS, (char_u *)"Insert"}, | |
2407 {K_INS, (char_u *)"Ins"}, /* Alternative name */ | |
2408 {K_KINS, (char_u *)"kInsert"}, | |
2409 {K_HOME, (char_u *)"Home"}, | |
2410 {K_KHOME, (char_u *)"kHome"}, | |
2411 {K_XHOME, (char_u *)"xHome"}, | |
230 | 2412 {K_ZHOME, (char_u *)"zHome"}, |
7 | 2413 {K_END, (char_u *)"End"}, |
2414 {K_KEND, (char_u *)"kEnd"}, | |
2415 {K_XEND, (char_u *)"xEnd"}, | |
230 | 2416 {K_ZEND, (char_u *)"zEnd"}, |
7 | 2417 {K_PAGEUP, (char_u *)"PageUp"}, |
2418 {K_PAGEDOWN, (char_u *)"PageDown"}, | |
2419 {K_KPAGEUP, (char_u *)"kPageUp"}, | |
2420 {K_KPAGEDOWN, (char_u *)"kPageDown"}, | |
2421 | |
2422 {K_KPLUS, (char_u *)"kPlus"}, | |
2423 {K_KMINUS, (char_u *)"kMinus"}, | |
2424 {K_KDIVIDE, (char_u *)"kDivide"}, | |
2425 {K_KMULTIPLY, (char_u *)"kMultiply"}, | |
2426 {K_KENTER, (char_u *)"kEnter"}, | |
2427 {K_KPOINT, (char_u *)"kPoint"}, | |
2428 | |
2429 {K_K0, (char_u *)"k0"}, | |
2430 {K_K1, (char_u *)"k1"}, | |
2431 {K_K2, (char_u *)"k2"}, | |
2432 {K_K3, (char_u *)"k3"}, | |
2433 {K_K4, (char_u *)"k4"}, | |
2434 {K_K5, (char_u *)"k5"}, | |
2435 {K_K6, (char_u *)"k6"}, | |
2436 {K_K7, (char_u *)"k7"}, | |
2437 {K_K8, (char_u *)"k8"}, | |
2438 {K_K9, (char_u *)"k9"}, | |
2439 | |
2440 {'<', (char_u *)"lt"}, | |
2441 | |
2442 {K_MOUSE, (char_u *)"Mouse"}, | |
3273 | 2443 #ifdef FEAT_MOUSE_NET |
7 | 2444 {K_NETTERM_MOUSE, (char_u *)"NetMouse"}, |
3273 | 2445 #endif |
2446 #ifdef FEAT_MOUSE_DEC | |
7 | 2447 {K_DEC_MOUSE, (char_u *)"DecMouse"}, |
3273 | 2448 #endif |
2449 #ifdef FEAT_MOUSE_JSB | |
7 | 2450 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"}, |
3273 | 2451 #endif |
2452 #ifdef FEAT_MOUSE_PTERM | |
7 | 2453 {K_PTERM_MOUSE, (char_u *)"PtermMouse"}, |
3273 | 2454 #endif |
2455 #ifdef FEAT_MOUSE_URXVT | |
2456 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"}, | |
2457 #endif | |
3746 | 2458 #ifdef FEAT_MOUSE_SGR |
2459 {K_SGR_MOUSE, (char_u *)"SgrMouse"}, | |
2460 #endif | |
7 | 2461 {K_LEFTMOUSE, (char_u *)"LeftMouse"}, |
2462 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"}, | |
2463 {K_LEFTDRAG, (char_u *)"LeftDrag"}, | |
2464 {K_LEFTRELEASE, (char_u *)"LeftRelease"}, | |
2465 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"}, | |
2466 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"}, | |
2467 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"}, | |
2468 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"}, | |
2469 {K_RIGHTMOUSE, (char_u *)"RightMouse"}, | |
2470 {K_RIGHTDRAG, (char_u *)"RightDrag"}, | |
2471 {K_RIGHTRELEASE, (char_u *)"RightRelease"}, | |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2363
diff
changeset
|
2472 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"}, |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2363
diff
changeset
|
2473 {K_MOUSEUP, (char_u *)"ScrollWheelDown"}, |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2363
diff
changeset
|
2474 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"}, |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2363
diff
changeset
|
2475 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"}, |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2363
diff
changeset
|
2476 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */ |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2363
diff
changeset
|
2477 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */ |
7 | 2478 {K_X1MOUSE, (char_u *)"X1Mouse"}, |
2479 {K_X1DRAG, (char_u *)"X1Drag"}, | |
2480 {K_X1RELEASE, (char_u *)"X1Release"}, | |
2481 {K_X2MOUSE, (char_u *)"X2Mouse"}, | |
2482 {K_X2DRAG, (char_u *)"X2Drag"}, | |
2483 {K_X2RELEASE, (char_u *)"X2Release"}, | |
2484 {K_DROP, (char_u *)"Drop"}, | |
2485 {K_ZERO, (char_u *)"Nul"}, | |
2486 #ifdef FEAT_EVAL | |
2487 {K_SNR, (char_u *)"SNR"}, | |
2488 #endif | |
2489 {K_PLUG, (char_u *)"Plug"}, | |
6245 | 2490 {K_CURSORHOLD, (char_u *)"CursorHold"}, |
7 | 2491 {0, NULL} |
2492 }; | |
2493 | |
2494 #define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry)) | |
2495 | |
2496 #ifdef FEAT_MOUSE | |
2497 static struct mousetable | |
2498 { | |
2499 int pseudo_code; /* Code for pseudo mouse event */ | |
2500 int button; /* Which mouse button is it? */ | |
2501 int is_click; /* Is it a mouse button click event? */ | |
2502 int is_drag; /* Is it a mouse drag event? */ | |
2503 } mouse_table[] = | |
2504 { | |
2505 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE}, | |
2506 #ifdef FEAT_GUI | |
2507 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE}, | |
2508 #endif | |
2509 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE}, | |
2510 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE}, | |
2511 #ifdef FEAT_GUI | |
2512 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE}, | |
2513 #endif | |
2514 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE}, | |
2515 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE}, | |
2516 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE}, | |
2517 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE}, | |
2518 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE}, | |
2519 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE}, | |
2520 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE}, | |
2521 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE}, | |
2522 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE}, | |
2523 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE}, | |
2524 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE}, | |
2525 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE}, | |
2526 /* DRAG without CLICK */ | |
2527 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE}, | |
2528 /* RELEASE without CLICK */ | |
2529 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE}, | |
2530 {0, 0, 0, 0}, | |
2531 }; | |
2532 #endif /* FEAT_MOUSE */ | |
2533 | |
2534 /* | |
2535 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given | |
2536 * modifier name ('S' for Shift, 'C' for Ctrl etc). | |
2537 */ | |
2538 int | |
2539 name_to_mod_mask(c) | |
2540 int c; | |
2541 { | |
2542 int i; | |
2543 | |
2544 c = TOUPPER_ASC(c); | |
2545 for (i = 0; mod_mask_table[i].mod_mask != 0; i++) | |
2546 if (c == mod_mask_table[i].name) | |
2547 return mod_mask_table[i].mod_flag; | |
2548 return 0; | |
2549 } | |
2550 | |
2551 /* | |
2552 * Check if if there is a special key code for "key" that includes the | |
2553 * modifiers specified. | |
2554 */ | |
2555 int | |
2556 simplify_key(key, modifiers) | |
2557 int key; | |
2558 int *modifiers; | |
2559 { | |
2560 int i; | |
2561 int key0; | |
2562 int key1; | |
2563 | |
2564 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT)) | |
2565 { | |
2566 /* TAB is a special case */ | |
2567 if (key == TAB && (*modifiers & MOD_MASK_SHIFT)) | |
2568 { | |
2569 *modifiers &= ~MOD_MASK_SHIFT; | |
2570 return K_S_TAB; | |
2571 } | |
2572 key0 = KEY2TERMCAP0(key); | |
2573 key1 = KEY2TERMCAP1(key); | |
2574 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE) | |
2575 if (key0 == modifier_keys_table[i + 3] | |
2576 && key1 == modifier_keys_table[i + 4] | |
2577 && (*modifiers & modifier_keys_table[i])) | |
2578 { | |
2579 *modifiers &= ~modifier_keys_table[i]; | |
2580 return TERMCAP2KEY(modifier_keys_table[i + 1], | |
2581 modifier_keys_table[i + 2]); | |
2582 } | |
2583 } | |
2584 return key; | |
2585 } | |
2586 | |
2587 /* | |
180 | 2588 * Change <xHome> to <Home>, <xUp> to <Up>, etc. |
2589 */ | |
2590 int | |
2591 handle_x_keys(key) | |
2592 int key; | |
2593 { | |
2594 switch (key) | |
2595 { | |
2596 case K_XUP: return K_UP; | |
2597 case K_XDOWN: return K_DOWN; | |
2598 case K_XLEFT: return K_LEFT; | |
2599 case K_XRIGHT: return K_RIGHT; | |
2600 case K_XHOME: return K_HOME; | |
230 | 2601 case K_ZHOME: return K_HOME; |
180 | 2602 case K_XEND: return K_END; |
230 | 2603 case K_ZEND: return K_END; |
180 | 2604 case K_XF1: return K_F1; |
2605 case K_XF2: return K_F2; | |
2606 case K_XF3: return K_F3; | |
2607 case K_XF4: return K_F4; | |
2608 case K_S_XF1: return K_S_F1; | |
2609 case K_S_XF2: return K_S_F2; | |
2610 case K_S_XF3: return K_S_F3; | |
2611 case K_S_XF4: return K_S_F4; | |
2612 } | |
2613 return key; | |
2614 } | |
2615 | |
2616 /* | |
7 | 2617 * Return a string which contains the name of the given key when the given |
2618 * modifiers are down. | |
2619 */ | |
2620 char_u * | |
2621 get_special_key_name(c, modifiers) | |
2622 int c; | |
2623 int modifiers; | |
2624 { | |
2625 static char_u string[MAX_KEY_NAME_LEN + 1]; | |
2626 | |
2627 int i, idx; | |
2628 int table_idx; | |
2629 char_u *s; | |
2630 | |
2631 string[0] = '<'; | |
2632 idx = 1; | |
2633 | |
2634 /* Key that stands for a normal character. */ | |
2635 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY) | |
2636 c = KEY2TERMCAP1(c); | |
2637 | |
2638 /* | |
2639 * Translate shifted special keys into unshifted keys and set modifier. | |
2640 * Same for CTRL and ALT modifiers. | |
2641 */ | |
2642 if (IS_SPECIAL(c)) | |
2643 { | |
2644 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE) | |
2645 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1] | |
2646 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2]) | |
2647 { | |
2648 modifiers |= modifier_keys_table[i]; | |
2649 c = TERMCAP2KEY(modifier_keys_table[i + 3], | |
2650 modifier_keys_table[i + 4]); | |
2651 break; | |
2652 } | |
2653 } | |
2654 | |
2655 /* try to find the key in the special key table */ | |
2656 table_idx = find_special_key_in_table(c); | |
2657 | |
2658 /* | |
2659 * When not a known special key, and not a printable character, try to | |
2660 * extract modifiers. | |
2661 */ | |
2662 if (c > 0 | |
2663 #ifdef FEAT_MBYTE | |
2664 && (*mb_char2len)(c) == 1 | |
2665 #endif | |
2666 ) | |
2667 { | |
2668 if (table_idx < 0 | |
2669 && (!vim_isprintc(c) || (c & 0x7f) == ' ') | |
2670 && (c & 0x80)) | |
2671 { | |
2672 c &= 0x7f; | |
2673 modifiers |= MOD_MASK_ALT; | |
2674 /* try again, to find the un-alted key in the special key table */ | |
2675 table_idx = find_special_key_in_table(c); | |
2676 } | |
2677 if (table_idx < 0 && !vim_isprintc(c) && c < ' ') | |
2678 { | |
2679 #ifdef EBCDIC | |
2680 c = CtrlChar(c); | |
2681 #else | |
2682 c += '@'; | |
2683 #endif | |
2684 modifiers |= MOD_MASK_CTRL; | |
2685 } | |
2686 } | |
2687 | |
2688 /* translate the modifier into a string */ | |
2689 for (i = 0; mod_mask_table[i].name != 'A'; i++) | |
2690 if ((modifiers & mod_mask_table[i].mod_mask) | |
2691 == mod_mask_table[i].mod_flag) | |
2692 { | |
2693 string[idx++] = mod_mask_table[i].name; | |
2694 string[idx++] = (char_u)'-'; | |
2695 } | |
2696 | |
2697 if (table_idx < 0) /* unknown special key, may output t_xx */ | |
2698 { | |
2699 if (IS_SPECIAL(c)) | |
2700 { | |
2701 string[idx++] = 't'; | |
2702 string[idx++] = '_'; | |
2703 string[idx++] = KEY2TERMCAP0(c); | |
2704 string[idx++] = KEY2TERMCAP1(c); | |
2705 } | |
2706 /* Not a special key, only modifiers, output directly */ | |
2707 else | |
2708 { | |
2709 #ifdef FEAT_MBYTE | |
2710 if (has_mbyte && (*mb_char2len)(c) > 1) | |
2711 idx += (*mb_char2bytes)(c, string + idx); | |
2712 else | |
2713 #endif | |
2714 if (vim_isprintc(c)) | |
2715 string[idx++] = c; | |
2716 else | |
2717 { | |
2718 s = transchar(c); | |
2719 while (*s) | |
2720 string[idx++] = *s++; | |
2721 } | |
2722 } | |
2723 } | |
2724 else /* use name of special key */ | |
2725 { | |
2726 STRCPY(string + idx, key_names_table[table_idx].name); | |
2727 idx = (int)STRLEN(string); | |
2728 } | |
2729 string[idx++] = '>'; | |
2730 string[idx] = NUL; | |
2731 return string; | |
2732 } | |
2733 | |
2734 /* | |
2735 * Try translating a <> name at (*srcp)[] to dst[]. | |
2736 * Return the number of characters added to dst[], zero for no match. | |
2737 * If there is a match, srcp is advanced to after the <> name. | |
2738 * dst[] must be big enough to hold the result (up to six characters)! | |
2739 */ | |
2740 int | |
2741 trans_special(srcp, dst, keycode) | |
2742 char_u **srcp; | |
2743 char_u *dst; | |
2744 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */ | |
2745 { | |
2746 int modifiers = 0; | |
2747 int key; | |
2748 int dlen = 0; | |
2749 | |
1775 | 2750 key = find_special_key(srcp, &modifiers, keycode, FALSE); |
7 | 2751 if (key == 0) |
2752 return 0; | |
2753 | |
2754 /* Put the appropriate modifier in a string */ | |
2755 if (modifiers != 0) | |
2756 { | |
2757 dst[dlen++] = K_SPECIAL; | |
2758 dst[dlen++] = KS_MODIFIER; | |
2759 dst[dlen++] = modifiers; | |
2760 } | |
2761 | |
2762 if (IS_SPECIAL(key)) | |
2763 { | |
2764 dst[dlen++] = K_SPECIAL; | |
2765 dst[dlen++] = KEY2TERMCAP0(key); | |
2766 dst[dlen++] = KEY2TERMCAP1(key); | |
2767 } | |
2768 #ifdef FEAT_MBYTE | |
2769 else if (has_mbyte && !keycode) | |
2770 dlen += (*mb_char2bytes)(key, dst + dlen); | |
2771 #endif | |
2772 else if (keycode) | |
2773 dlen = (int)(add_char2buf(key, dst + dlen) - dst); | |
2774 else | |
2775 dst[dlen++] = key; | |
2776 | |
2777 return dlen; | |
2778 } | |
2779 | |
2780 /* | |
2781 * Try translating a <> name at (*srcp)[], return the key and modifiers. | |
2782 * srcp is advanced to after the <> name. | |
2783 * returns 0 if there is no match. | |
2784 */ | |
2785 int | |
1775 | 2786 find_special_key(srcp, modp, keycode, keep_x_key) |
7 | 2787 char_u **srcp; |
2788 int *modp; | |
1775 | 2789 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */ |
2790 int keep_x_key; /* don't translate xHome to Home key */ | |
7 | 2791 { |
2792 char_u *last_dash; | |
2793 char_u *end_of_name; | |
2794 char_u *src; | |
2795 char_u *bp; | |
2796 int modifiers; | |
2797 int bit; | |
2798 int key; | |
835 | 2799 unsigned long n; |
3024 | 2800 int l; |
7 | 2801 |
2802 src = *srcp; | |
2803 if (src[0] != '<') | |
2804 return 0; | |
2805 | |
2806 /* Find end of modifier list */ | |
2807 last_dash = src; | |
2808 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++) | |
2809 { | |
2810 if (*bp == '-') | |
2811 { | |
2812 last_dash = bp; | |
3024 | 2813 if (bp[1] != NUL) |
2814 { | |
2815 #ifdef FEAT_MBYTE | |
2816 if (has_mbyte) | |
2817 l = mb_ptr2len(bp + 1); | |
2818 else | |
2819 #endif | |
2820 l = 1; | |
2821 if (bp[l + 1] == '>') | |
2822 bp += l; /* anything accepted, like <C-?> */ | |
2823 } | |
7 | 2824 } |
2825 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3]) | |
2826 bp += 3; /* skip t_xx, xx may be '-' or '>' */ | |
3026 | 2827 else if (STRNICMP(bp, "char-", 5) == 0) |
2828 { | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
2829 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0); |
3026 | 2830 bp += l + 5; |
2831 break; | |
2832 } | |
7 | 2833 } |
2834 | |
2835 if (*bp == '>') /* found matching '>' */ | |
2836 { | |
2837 end_of_name = bp + 1; | |
2838 | |
2839 /* Which modifiers are given? */ | |
2840 modifiers = 0x0; | |
2841 for (bp = src + 1; bp < last_dash; bp++) | |
2842 { | |
2843 if (*bp != '-') | |
2844 { | |
2845 bit = name_to_mod_mask(*bp); | |
2846 if (bit == 0x0) | |
2847 break; /* Illegal modifier name */ | |
2848 modifiers |= bit; | |
2849 } | |
2850 } | |
2851 | |
2852 /* | |
2853 * Legal modifier name. | |
2854 */ | |
2855 if (bp >= last_dash) | |
2856 { | |
3024 | 2857 if (STRNICMP(last_dash + 1, "char-", 5) == 0 |
2858 && VIM_ISDIGIT(last_dash[6])) | |
2859 { | |
2860 /* <Char-123> or <Char-033> or <Char-0x33> */ | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
2861 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0); |
3026 | 2862 key = (int)n; |
3024 | 2863 } |
7 | 2864 else |
180 | 2865 { |
3026 | 2866 /* |
2867 * Modifier with single letter, or special key name. | |
2868 */ | |
2869 #ifdef FEAT_MBYTE | |
2870 if (has_mbyte) | |
2871 l = mb_ptr2len(last_dash + 1); | |
2872 else | |
2873 #endif | |
2874 l = 1; | |
2875 if (modifiers != 0 && last_dash[l + 1] == '>') | |
2876 key = PTR2CHAR(last_dash + 1); | |
2877 else | |
2878 { | |
2879 key = get_special_key_code(last_dash + 1); | |
2880 if (!keep_x_key) | |
2881 key = handle_x_keys(key); | |
2882 } | |
180 | 2883 } |
7 | 2884 |
2885 /* | |
2886 * get_special_key_code() may return NUL for invalid | |
2887 * special key name. | |
2888 */ | |
2889 if (key != NUL) | |
2890 { | |
2891 /* | |
2892 * Only use a modifier when there is no special key code that | |
2893 * includes the modifier. | |
2894 */ | |
2895 key = simplify_key(key, &modifiers); | |
2896 | |
2897 if (!keycode) | |
2898 { | |
2899 /* don't want keycode, use single byte code */ | |
2900 if (key == K_BS) | |
2901 key = BS; | |
2902 else if (key == K_DEL || key == K_KDEL) | |
2903 key = DEL; | |
2904 } | |
2905 | |
2906 /* | |
2907 * Normal Key with modifier: Try to make a single byte code. | |
2908 */ | |
2909 if (!IS_SPECIAL(key)) | |
2910 key = extract_modifiers(key, &modifiers); | |
2911 | |
2912 *modp = modifiers; | |
2913 *srcp = end_of_name; | |
2914 return key; | |
2915 } | |
2916 } | |
2917 } | |
2918 return 0; | |
2919 } | |
2920 | |
2921 /* | |
2922 * Try to include modifiers in the key. | |
2923 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc. | |
2924 */ | |
2925 int | |
2926 extract_modifiers(key, modp) | |
2927 int key; | |
2928 int *modp; | |
2929 { | |
2930 int modifiers = *modp; | |
2931 | |
2932 #ifdef MACOS | |
4352 | 2933 /* Command-key really special, no fancynest */ |
7 | 2934 if (!(modifiers & MOD_MASK_CMD)) |
2935 #endif | |
2936 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key)) | |
2937 { | |
2938 key = TOUPPER_ASC(key); | |
2939 modifiers &= ~MOD_MASK_SHIFT; | |
2940 } | |
2941 if ((modifiers & MOD_MASK_CTRL) | |
2942 #ifdef EBCDIC | |
2943 /* * TODO: EBCDIC Better use: | |
2944 * && (Ctrl_chr(key) || key == '?') | |
2945 * ??? */ | |
2946 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key) | |
2947 != NULL | |
2948 #else | |
2949 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key)) | |
2950 #endif | |
2951 ) | |
2952 { | |
2953 key = Ctrl_chr(key); | |
2954 modifiers &= ~MOD_MASK_CTRL; | |
2955 /* <C-@> is <Nul> */ | |
2956 if (key == 0) | |
2957 key = K_ZERO; | |
2958 } | |
2959 #ifdef MACOS | |
4352 | 2960 /* Command-key really special, no fancynest */ |
7 | 2961 if (!(modifiers & MOD_MASK_CMD)) |
2962 #endif | |
2963 if ((modifiers & MOD_MASK_ALT) && key < 0x80 | |
2964 #ifdef FEAT_MBYTE | |
2965 && !enc_dbcs /* avoid creating a lead byte */ | |
2966 #endif | |
2967 ) | |
2968 { | |
2969 key |= 0x80; | |
2970 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */ | |
2971 } | |
2972 | |
2973 *modp = modifiers; | |
2974 return key; | |
2975 } | |
2976 | |
2977 /* | |
2978 * Try to find key "c" in the special key table. | |
2979 * Return the index when found, -1 when not found. | |
2980 */ | |
2981 int | |
2982 find_special_key_in_table(c) | |
2983 int c; | |
2984 { | |
2985 int i; | |
2986 | |
2987 for (i = 0; key_names_table[i].name != NULL; i++) | |
2988 if (c == key_names_table[i].key) | |
2989 break; | |
2990 if (key_names_table[i].name == NULL) | |
2991 i = -1; | |
2992 return i; | |
2993 } | |
2994 | |
2995 /* | |
2996 * Find the special key with the given name (the given string does not have to | |
2997 * end with NUL, the name is assumed to end before the first non-idchar). | |
2998 * If the name starts with "t_" the next two characters are interpreted as a | |
2999 * termcap name. | |
3000 * Return the key code, or 0 if not found. | |
3001 */ | |
3002 int | |
3003 get_special_key_code(name) | |
3004 char_u *name; | |
3005 { | |
3006 char_u *table_name; | |
3007 char_u string[3]; | |
3008 int i, j; | |
3009 | |
3010 /* | |
3011 * If it's <t_xx> we get the code for xx from the termcap | |
3012 */ | |
3013 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL) | |
3014 { | |
3015 string[0] = name[2]; | |
3016 string[1] = name[3]; | |
3017 string[2] = NUL; | |
3018 if (add_termcap_entry(string, FALSE) == OK) | |
3019 return TERMCAP2KEY(name[2], name[3]); | |
3020 } | |
3021 else | |
3022 for (i = 0; key_names_table[i].name != NULL; i++) | |
3023 { | |
3024 table_name = key_names_table[i].name; | |
3025 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++) | |
3026 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) | |
3027 break; | |
3028 if (!vim_isIDc(name[j]) && table_name[j] == NUL) | |
3029 return key_names_table[i].key; | |
3030 } | |
3031 return 0; | |
3032 } | |
3033 | |
1661 | 3034 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
7 | 3035 char_u * |
3036 get_key_name(i) | |
3037 int i; | |
3038 { | |
1881 | 3039 if (i >= (int)KEY_NAMES_TABLE_LEN) |
7 | 3040 return NULL; |
3041 return key_names_table[i].name; | |
3042 } | |
3043 #endif | |
3044 | |
1661 | 3045 #if defined(FEAT_MOUSE) || defined(PROTO) |
7 | 3046 /* |
3047 * Look up the given mouse code to return the relevant information in the other | |
3048 * arguments. Return which button is down or was released. | |
3049 */ | |
3050 int | |
3051 get_mouse_button(code, is_click, is_drag) | |
3052 int code; | |
3053 int *is_click; | |
3054 int *is_drag; | |
3055 { | |
3056 int i; | |
3057 | |
3058 for (i = 0; mouse_table[i].pseudo_code; i++) | |
3059 if (code == mouse_table[i].pseudo_code) | |
3060 { | |
3061 *is_click = mouse_table[i].is_click; | |
3062 *is_drag = mouse_table[i].is_drag; | |
3063 return mouse_table[i].button; | |
3064 } | |
3065 return 0; /* Shouldn't get here */ | |
3066 } | |
3067 | |
3068 /* | |
3069 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on | |
3070 * the given information about which mouse button is down, and whether the | |
3071 * mouse was clicked, dragged or released. | |
3072 */ | |
3073 int | |
3074 get_pseudo_mouse_code(button, is_click, is_drag) | |
3075 int button; /* eg MOUSE_LEFT */ | |
3076 int is_click; | |
3077 int is_drag; | |
3078 { | |
3079 int i; | |
3080 | |
3081 for (i = 0; mouse_table[i].pseudo_code; i++) | |
3082 if (button == mouse_table[i].button | |
3083 && is_click == mouse_table[i].is_click | |
3084 && is_drag == mouse_table[i].is_drag) | |
3085 { | |
3086 #ifdef FEAT_GUI | |
263 | 3087 /* Trick: a non mappable left click and release has mouse_col -1 |
3088 * or added MOUSE_COLOFF. Used for 'mousefocus' in | |
3089 * gui_mouse_moved() */ | |
3090 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF) | |
7 | 3091 { |
263 | 3092 if (mouse_col < 0) |
3093 mouse_col = 0; | |
3094 else | |
3095 mouse_col -= MOUSE_COLOFF; | |
7 | 3096 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE) |
3097 return (int)KE_LEFTMOUSE_NM; | |
3098 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE) | |
3099 return (int)KE_LEFTRELEASE_NM; | |
3100 } | |
3101 #endif | |
3102 return mouse_table[i].pseudo_code; | |
3103 } | |
1209 | 3104 return (int)KE_IGNORE; /* not recognized, ignore it */ |
7 | 3105 } |
3106 #endif /* FEAT_MOUSE */ | |
3107 | |
3108 /* | |
3109 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC. | |
3110 */ | |
3111 int | |
3112 get_fileformat(buf) | |
3113 buf_T *buf; | |
3114 { | |
3115 int c = *buf->b_p_ff; | |
3116 | |
3117 if (buf->b_p_bin || c == 'u') | |
3118 return EOL_UNIX; | |
3119 if (c == 'm') | |
3120 return EOL_MAC; | |
3121 return EOL_DOS; | |
3122 } | |
3123 | |
3124 /* | |
3125 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val" | |
3126 * argument. | |
3127 */ | |
3128 int | |
3129 get_fileformat_force(buf, eap) | |
3130 buf_T *buf; | |
3131 exarg_T *eap; /* can be NULL! */ | |
3132 { | |
3133 int c; | |
3134 | |
3135 if (eap != NULL && eap->force_ff != 0) | |
3136 c = eap->cmd[eap->force_ff]; | |
3137 else | |
3138 { | |
3139 if ((eap != NULL && eap->force_bin != 0) | |
3140 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin) | |
3141 return EOL_UNIX; | |
3142 c = *buf->b_p_ff; | |
3143 } | |
3144 if (c == 'u') | |
3145 return EOL_UNIX; | |
3146 if (c == 'm') | |
3147 return EOL_MAC; | |
3148 return EOL_DOS; | |
3149 } | |
3150 | |
3151 /* | |
3152 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC. | |
3153 * Sets both 'textmode' and 'fileformat'. | |
3154 * Note: Does _not_ set global value of 'textmode'! | |
3155 */ | |
3156 void | |
3157 set_fileformat(t, opt_flags) | |
3158 int t; | |
3159 int opt_flags; /* OPT_LOCAL and/or OPT_GLOBAL */ | |
3160 { | |
3161 char *p = NULL; | |
3162 | |
3163 switch (t) | |
3164 { | |
3165 case EOL_DOS: | |
3166 p = FF_DOS; | |
3167 curbuf->b_p_tx = TRUE; | |
3168 break; | |
3169 case EOL_UNIX: | |
3170 p = FF_UNIX; | |
3171 curbuf->b_p_tx = FALSE; | |
3172 break; | |
3173 case EOL_MAC: | |
3174 p = FF_MAC; | |
3175 curbuf->b_p_tx = FALSE; | |
3176 break; | |
3177 } | |
3178 if (p != NULL) | |
3179 set_string_option_direct((char_u *)"ff", -1, (char_u *)p, | |
694 | 3180 OPT_FREE | opt_flags, 0); |
3181 | |
7 | 3182 #ifdef FEAT_WINDOWS |
671 | 3183 /* This may cause the buffer to become (un)modified. */ |
7 | 3184 check_status(curbuf); |
673 | 3185 redraw_tabline = TRUE; |
7 | 3186 #endif |
3187 #ifdef FEAT_TITLE | |
3188 need_maketitle = TRUE; /* set window title later */ | |
3189 #endif | |
3190 } | |
3191 | |
3192 /* | |
3193 * Return the default fileformat from 'fileformats'. | |
3194 */ | |
3195 int | |
3196 default_fileformat() | |
3197 { | |
3198 switch (*p_ffs) | |
3199 { | |
3200 case 'm': return EOL_MAC; | |
3201 case 'd': return EOL_DOS; | |
3202 } | |
3203 return EOL_UNIX; | |
3204 } | |
3205 | |
3206 /* | |
3207 * Call shell. Calls mch_call_shell, with 'shellxquote' added. | |
3208 */ | |
3209 int | |
3210 call_shell(cmd, opt) | |
3211 char_u *cmd; | |
3212 int opt; | |
3213 { | |
3214 char_u *ncmd; | |
3215 int retval; | |
170 | 3216 #ifdef FEAT_PROFILE |
3217 proftime_T wait_time; | |
3218 #endif | |
7 | 3219 |
3220 if (p_verbose > 3) | |
3221 { | |
293 | 3222 verbose_enter(); |
273 | 3223 smsg((char_u *)_("Calling shell to execute: \"%s\""), |
7 | 3224 cmd == NULL ? p_sh : cmd); |
3225 out_char('\n'); | |
3226 cursor_on(); | |
293 | 3227 verbose_leave(); |
7 | 3228 } |
3229 | |
170 | 3230 #ifdef FEAT_PROFILE |
789 | 3231 if (do_profiling == PROF_YES) |
170 | 3232 prof_child_enter(&wait_time); |
3233 #endif | |
3234 | |
7 | 3235 if (*p_sh == NUL) |
3236 { | |
3237 EMSG(_(e_shellempty)); | |
3238 retval = -1; | |
3239 } | |
3240 else | |
3241 { | |
3242 #ifdef FEAT_GUI_MSWIN | |
3243 /* Don't hide the pointer while executing a shell command. */ | |
3244 gui_mch_mousehide(FALSE); | |
3245 #endif | |
3246 #ifdef FEAT_GUI | |
3247 ++hold_gui_events; | |
3248 #endif | |
3249 /* The external command may update a tags file, clear cached tags. */ | |
3250 tag_freematch(); | |
3251 | |
3252 if (cmd == NULL || *p_sxq == NUL) | |
3253 retval = mch_call_shell(cmd, opt); | |
3254 else | |
3255 { | |
3359 | 3256 char_u *ecmd = cmd; |
3257 | |
3258 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0) | |
3259 { | |
3260 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE); | |
3261 if (ecmd == NULL) | |
3262 ecmd = cmd; | |
3263 } | |
3264 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1)); | |
7 | 3265 if (ncmd != NULL) |
3266 { | |
3267 STRCPY(ncmd, p_sxq); | |
3359 | 3268 STRCAT(ncmd, ecmd); |
3357 | 3269 /* When 'shellxquote' is ( append ). |
3270 * When 'shellxquote' is "( append )". */ | |
3271 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")" | |
3272 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\"" | |
3273 : p_sxq); | |
7 | 3274 retval = mch_call_shell(ncmd, opt); |
3275 vim_free(ncmd); | |
3276 } | |
3277 else | |
3278 retval = -1; | |
3359 | 3279 if (ecmd != cmd) |
3280 vim_free(ecmd); | |
7 | 3281 } |
3282 #ifdef FEAT_GUI | |
3283 --hold_gui_events; | |
3284 #endif | |
3285 /* | |
3286 * Check the window size, in case it changed while executing the | |
3287 * external command. | |
3288 */ | |
3289 shell_resized_check(); | |
3290 } | |
3291 | |
3292 #ifdef FEAT_EVAL | |
3293 set_vim_var_nr(VV_SHELL_ERROR, (long)retval); | |
170 | 3294 # ifdef FEAT_PROFILE |
789 | 3295 if (do_profiling == PROF_YES) |
170 | 3296 prof_child_exit(&wait_time); |
3297 # endif | |
7 | 3298 #endif |
3299 | |
3300 return retval; | |
3301 } | |
3302 | |
3303 /* | |
789 | 3304 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to |
3305 * NORMAL State with a condition. This function returns the real State. | |
7 | 3306 */ |
3307 int | |
3308 get_real_state() | |
3309 { | |
3310 if (State & NORMAL) | |
3311 { | |
3312 if (VIsual_active) | |
789 | 3313 { |
3314 if (VIsual_select) | |
3315 return SELECTMODE; | |
7 | 3316 return VISUAL; |
789 | 3317 } |
5735 | 3318 else if (finish_op) |
3319 return OP_PENDING; | |
7 | 3320 } |
3321 return State; | |
3322 } | |
3323 | |
39 | 3324 #if defined(FEAT_MBYTE) || defined(PROTO) |
3325 /* | |
3326 * Return TRUE if "p" points to just after a path separator. | |
2939 | 3327 * Takes care of multi-byte characters. |
39 | 3328 * "b" must point to the start of the file name |
3329 */ | |
3330 int | |
3331 after_pathsep(b, p) | |
3332 char_u *b; | |
3333 char_u *p; | |
3334 { | |
2939 | 3335 return p > b && vim_ispathsep(p[-1]) |
39 | 3336 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0); |
3337 } | |
3338 #endif | |
3339 | |
3340 /* | |
3341 * Return TRUE if file names "f1" and "f2" are in the same directory. | |
3342 * "f1" may be a short name, "f2" must be a full path. | |
3343 */ | |
3344 int | |
3345 same_directory(f1, f2) | |
3346 char_u *f1; | |
3347 char_u *f2; | |
3348 { | |
3349 char_u ffname[MAXPATHL]; | |
3350 char_u *t1; | |
3351 char_u *t2; | |
3352 | |
3353 /* safety check */ | |
3354 if (f1 == NULL || f2 == NULL) | |
3355 return FALSE; | |
3356 | |
3357 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE); | |
3358 t1 = gettail_sep(ffname); | |
3359 t2 = gettail_sep(f2); | |
3360 return (t1 - ffname == t2 - f2 | |
3361 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0); | |
3362 } | |
3363 | |
7 | 3364 #if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \ |
574 | 3365 || ((defined(FEAT_GUI_GTK)) \ |
11 | 3366 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \ |
7 | 3367 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \ |
3368 || defined(PROTO) | |
3369 /* | |
3370 * Change to a file's directory. | |
3371 * Caller must call shorten_fnames()! | |
3372 * Return OK or FAIL. | |
3373 */ | |
3374 int | |
3375 vim_chdirfile(fname) | |
3376 char_u *fname; | |
3377 { | |
39 | 3378 char_u dir[MAXPATHL]; |
3379 | |
418 | 3380 vim_strncpy(dir, fname, MAXPATHL - 1); |
39 | 3381 *gettail_sep(dir) = NUL; |
3382 return mch_chdir((char *)dir) == 0 ? OK : FAIL; | |
7 | 3383 } |
3384 #endif | |
3385 | |
3386 #if defined(STAT_IGNORES_SLASH) || defined(PROTO) | |
3387 /* | |
3388 * Check if "name" ends in a slash and is not a directory. | |
3389 * Used for systems where stat() ignores a trailing slash on a file name. | |
3390 * The Vim code assumes a trailing slash is only ignored for a directory. | |
3391 */ | |
3392 int | |
3393 illegal_slash(name) | |
3394 char *name; | |
3395 { | |
3396 if (name[0] == NUL) | |
3397 return FALSE; /* no file name is not illegal */ | |
3398 if (name[strlen(name) - 1] != '/') | |
3399 return FALSE; /* no trailing slash */ | |
3400 if (mch_isdir((char_u *)name)) | |
3401 return FALSE; /* trailing slash for a directory */ | |
3402 return TRUE; | |
3403 } | |
3404 #endif | |
3405 | |
3406 #if defined(CURSOR_SHAPE) || defined(PROTO) | |
3407 | |
3408 /* | |
3409 * Handling of cursor and mouse pointer shapes in various modes. | |
3410 */ | |
3411 | |
3412 cursorentry_T shape_table[SHAPE_IDX_COUNT] = | |
3413 { | |
3414 /* The values will be filled in from the 'guicursor' and 'mouseshape' | |
3415 * defaults when Vim starts. | |
3416 * Adjust the SHAPE_IDX_ defines when making changes! */ | |
3417 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3418 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3419 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3420 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3421 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3422 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3423 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3424 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3425 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE}, | |
3426 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE}, | |
3427 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE}, | |
3428 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE}, | |
3429 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE}, | |
3430 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE}, | |
3431 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE}, | |
3432 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE}, | |
3433 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR}, | |
3434 }; | |
3435 | |
3436 #ifdef FEAT_MOUSESHAPE | |
3437 /* | |
3438 * Table with names for mouse shapes. Keep in sync with all the tables for | |
3439 * mch_set_mouse_shape()!. | |
3440 */ | |
3441 static char * mshape_names[] = | |
3442 { | |
3443 "arrow", /* default, must be the first one */ | |
3444 "blank", /* hidden */ | |
3445 "beam", | |
3446 "updown", | |
3447 "udsizing", | |
3448 "leftright", | |
3449 "lrsizing", | |
3450 "busy", | |
3451 "no", | |
3452 "crosshair", | |
3453 "hand1", | |
3454 "hand2", | |
3455 "pencil", | |
3456 "question", | |
3457 "rightup-arrow", | |
3458 "up-arrow", | |
3459 NULL | |
3460 }; | |
3461 #endif | |
3462 | |
3463 /* | |
3464 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape' | |
3465 * ("what" is SHAPE_MOUSE). | |
3466 * Returns error message for an illegal option, NULL otherwise. | |
3467 */ | |
3468 char_u * | |
3469 parse_shape_opt(what) | |
3470 int what; | |
3471 { | |
3472 char_u *modep; | |
3473 char_u *colonp; | |
3474 char_u *commap; | |
3475 char_u *slashp; | |
3476 char_u *p, *endp; | |
3477 int idx = 0; /* init for GCC */ | |
3478 int all_idx; | |
3479 int len; | |
3480 int i; | |
3481 long n; | |
3482 int found_ve = FALSE; /* found "ve" flag */ | |
3483 int round; | |
3484 | |
3485 /* | |
3486 * First round: check for errors; second round: do it for real. | |
3487 */ | |
3488 for (round = 1; round <= 2; ++round) | |
3489 { | |
3490 /* | |
3491 * Repeat for all comma separated parts. | |
3492 */ | |
3493 #ifdef FEAT_MOUSESHAPE | |
3494 if (what == SHAPE_MOUSE) | |
3495 modep = p_mouseshape; | |
3496 else | |
3497 #endif | |
3498 modep = p_guicursor; | |
3499 while (*modep != NUL) | |
3500 { | |
3501 colonp = vim_strchr(modep, ':'); | |
3502 if (colonp == NULL) | |
3503 return (char_u *)N_("E545: Missing colon"); | |
3504 if (colonp == modep) | |
3505 return (char_u *)N_("E546: Illegal mode"); | |
3506 commap = vim_strchr(modep, ','); | |
3507 | |
3508 /* | |
3509 * Repeat for all mode's before the colon. | |
3510 * For the 'a' mode, we loop to handle all the modes. | |
3511 */ | |
3512 all_idx = -1; | |
3513 while (modep < colonp || all_idx >= 0) | |
3514 { | |
3515 if (all_idx < 0) | |
3516 { | |
3517 /* Find the mode. */ | |
3518 if (modep[1] == '-' || modep[1] == ':') | |
3519 len = 1; | |
3520 else | |
3521 len = 2; | |
3522 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a') | |
3523 all_idx = SHAPE_IDX_COUNT - 1; | |
3524 else | |
3525 { | |
3526 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx) | |
3527 if (STRNICMP(modep, shape_table[idx].name, len) | |
3528 == 0) | |
3529 break; | |
3530 if (idx == SHAPE_IDX_COUNT | |
3531 || (shape_table[idx].used_for & what) == 0) | |
3532 return (char_u *)N_("E546: Illegal mode"); | |
3533 if (len == 2 && modep[0] == 'v' && modep[1] == 'e') | |
3534 found_ve = TRUE; | |
3535 } | |
3536 modep += len + 1; | |
3537 } | |
3538 | |
3539 if (all_idx >= 0) | |
3540 idx = all_idx--; | |
3541 else if (round == 2) | |
3542 { | |
3543 #ifdef FEAT_MOUSESHAPE | |
3544 if (what == SHAPE_MOUSE) | |
3545 { | |
3546 /* Set the default, for the missing parts */ | |
3547 shape_table[idx].mshape = 0; | |
3548 } | |
3549 else | |
3550 #endif | |
3551 { | |
3552 /* Set the defaults, for the missing parts */ | |
3553 shape_table[idx].shape = SHAPE_BLOCK; | |
3554 shape_table[idx].blinkwait = 700L; | |
3555 shape_table[idx].blinkon = 400L; | |
3556 shape_table[idx].blinkoff = 250L; | |
3557 } | |
3558 } | |
3559 | |
3560 /* Parse the part after the colon */ | |
3561 for (p = colonp + 1; *p && *p != ','; ) | |
3562 { | |
3563 #ifdef FEAT_MOUSESHAPE | |
3564 if (what == SHAPE_MOUSE) | |
3565 { | |
3566 for (i = 0; ; ++i) | |
3567 { | |
3568 if (mshape_names[i] == NULL) | |
3569 { | |
3570 if (!VIM_ISDIGIT(*p)) | |
3571 return (char_u *)N_("E547: Illegal mouseshape"); | |
3572 if (round == 2) | |
3573 shape_table[idx].mshape = | |
3574 getdigits(&p) + MSHAPE_NUMBERED; | |
3575 else | |
3576 (void)getdigits(&p); | |
3577 break; | |
3578 } | |
3579 len = (int)STRLEN(mshape_names[i]); | |
3580 if (STRNICMP(p, mshape_names[i], len) == 0) | |
3581 { | |
3582 if (round == 2) | |
3583 shape_table[idx].mshape = i; | |
3584 p += len; | |
3585 break; | |
3586 } | |
3587 } | |
3588 } | |
3589 else /* if (what == SHAPE_MOUSE) */ | |
3590 #endif | |
3591 { | |
3592 /* | |
3593 * First handle the ones with a number argument. | |
3594 */ | |
3595 i = *p; | |
3596 len = 0; | |
3597 if (STRNICMP(p, "ver", 3) == 0) | |
3598 len = 3; | |
3599 else if (STRNICMP(p, "hor", 3) == 0) | |
3600 len = 3; | |
3601 else if (STRNICMP(p, "blinkwait", 9) == 0) | |
3602 len = 9; | |
3603 else if (STRNICMP(p, "blinkon", 7) == 0) | |
3604 len = 7; | |
3605 else if (STRNICMP(p, "blinkoff", 8) == 0) | |
3606 len = 8; | |
3607 if (len != 0) | |
3608 { | |
3609 p += len; | |
3610 if (!VIM_ISDIGIT(*p)) | |
3611 return (char_u *)N_("E548: digit expected"); | |
3612 n = getdigits(&p); | |
3613 if (len == 3) /* "ver" or "hor" */ | |
3614 { | |
3615 if (n == 0) | |
3616 return (char_u *)N_("E549: Illegal percentage"); | |
3617 if (round == 2) | |
3618 { | |
3619 if (TOLOWER_ASC(i) == 'v') | |
3620 shape_table[idx].shape = SHAPE_VER; | |
3621 else | |
3622 shape_table[idx].shape = SHAPE_HOR; | |
3623 shape_table[idx].percentage = n; | |
3624 } | |
3625 } | |
3626 else if (round == 2) | |
3627 { | |
3628 if (len == 9) | |
3629 shape_table[idx].blinkwait = n; | |
3630 else if (len == 7) | |
3631 shape_table[idx].blinkon = n; | |
3632 else | |
3633 shape_table[idx].blinkoff = n; | |
3634 } | |
3635 } | |
3636 else if (STRNICMP(p, "block", 5) == 0) | |
3637 { | |
3638 if (round == 2) | |
3639 shape_table[idx].shape = SHAPE_BLOCK; | |
3640 p += 5; | |
3641 } | |
3642 else /* must be a highlight group name then */ | |
3643 { | |
3644 endp = vim_strchr(p, '-'); | |
3645 if (commap == NULL) /* last part */ | |
3646 { | |
3647 if (endp == NULL) | |
3648 endp = p + STRLEN(p); /* find end of part */ | |
3649 } | |
3650 else if (endp > commap || endp == NULL) | |
3651 endp = commap; | |
3652 slashp = vim_strchr(p, '/'); | |
3653 if (slashp != NULL && slashp < endp) | |
3654 { | |
3655 /* "group/langmap_group" */ | |
3656 i = syn_check_group(p, (int)(slashp - p)); | |
3657 p = slashp + 1; | |
3658 } | |
3659 if (round == 2) | |
3660 { | |
3661 shape_table[idx].id = syn_check_group(p, | |
3662 (int)(endp - p)); | |
3663 shape_table[idx].id_lm = shape_table[idx].id; | |
3664 if (slashp != NULL && slashp < endp) | |
3665 shape_table[idx].id = i; | |
3666 } | |
3667 p = endp; | |
3668 } | |
3669 } /* if (what != SHAPE_MOUSE) */ | |
3670 | |
3671 if (*p == '-') | |
3672 ++p; | |
3673 } | |
3674 } | |
3675 modep = p; | |
3676 if (*modep == ',') | |
3677 ++modep; | |
3678 } | |
3679 } | |
3680 | |
3681 /* If the 's' flag is not given, use the 'v' cursor for 's' */ | |
3682 if (!found_ve) | |
3683 { | |
3684 #ifdef FEAT_MOUSESHAPE | |
3685 if (what == SHAPE_MOUSE) | |
3686 { | |
3687 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape; | |
3688 } | |
3689 else | |
3690 #endif | |
3691 { | |
3692 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape; | |
3693 shape_table[SHAPE_IDX_VE].percentage = | |
3694 shape_table[SHAPE_IDX_V].percentage; | |
3695 shape_table[SHAPE_IDX_VE].blinkwait = | |
3696 shape_table[SHAPE_IDX_V].blinkwait; | |
3697 shape_table[SHAPE_IDX_VE].blinkon = | |
3698 shape_table[SHAPE_IDX_V].blinkon; | |
3699 shape_table[SHAPE_IDX_VE].blinkoff = | |
3700 shape_table[SHAPE_IDX_V].blinkoff; | |
3701 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id; | |
3702 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm; | |
3703 } | |
3704 } | |
3705 | |
3706 return NULL; | |
3707 } | |
3708 | |
500 | 3709 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
3710 || defined(FEAT_MOUSESHAPE) || defined(PROTO) | |
7 | 3711 /* |
3712 * Return the index into shape_table[] for the current mode. | |
3713 * When "mouse" is TRUE, consider indexes valid for the mouse pointer. | |
3714 */ | |
3715 int | |
3716 get_shape_idx(mouse) | |
3717 int mouse; | |
3718 { | |
3719 #ifdef FEAT_MOUSESHAPE | |
3720 if (mouse && (State == HITRETURN || State == ASKMORE)) | |
3721 { | |
3722 # ifdef FEAT_GUI | |
87 | 3723 int x, y; |
3724 gui_mch_getmouse(&x, &y); | |
3725 if (Y_2_ROW(y) == Rows - 1) | |
7 | 3726 return SHAPE_IDX_MOREL; |
3727 # endif | |
3728 return SHAPE_IDX_MORE; | |
3729 } | |
3730 if (mouse && drag_status_line) | |
3731 return SHAPE_IDX_SDRAG; | |
3732 # ifdef FEAT_VERTSPLIT | |
3733 if (mouse && drag_sep_line) | |
3734 return SHAPE_IDX_VDRAG; | |
3735 # endif | |
3736 #endif | |
3737 if (!mouse && State == SHOWMATCH) | |
3738 return SHAPE_IDX_SM; | |
3739 #ifdef FEAT_VREPLACE | |
3740 if (State & VREPLACE_FLAG) | |
3741 return SHAPE_IDX_R; | |
3742 #endif | |
3743 if (State & REPLACE_FLAG) | |
3744 return SHAPE_IDX_R; | |
3745 if (State & INSERT) | |
3746 return SHAPE_IDX_I; | |
3747 if (State & CMDLINE) | |
3748 { | |
3749 if (cmdline_at_end()) | |
3750 return SHAPE_IDX_C; | |
3751 if (cmdline_overstrike()) | |
3752 return SHAPE_IDX_CR; | |
3753 return SHAPE_IDX_CI; | |
3754 } | |
3755 if (finish_op) | |
3756 return SHAPE_IDX_O; | |
3757 if (VIsual_active) | |
3758 { | |
3759 if (*p_sel == 'e') | |
3760 return SHAPE_IDX_VE; | |
3761 else | |
3762 return SHAPE_IDX_V; | |
3763 } | |
3764 return SHAPE_IDX_N; | |
3765 } | |
500 | 3766 #endif |
7 | 3767 |
3768 # if defined(FEAT_MOUSESHAPE) || defined(PROTO) | |
3769 static int old_mouse_shape = 0; | |
3770 | |
3771 /* | |
3772 * Set the mouse shape: | |
3773 * If "shape" is -1, use shape depending on the current mode, | |
3774 * depending on the current state. | |
3775 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used | |
3776 * when the mouse moves off the status or command line). | |
3777 */ | |
3778 void | |
3779 update_mouseshape(shape_idx) | |
3780 int shape_idx; | |
3781 { | |
3782 int new_mouse_shape; | |
3783 | |
3784 /* Only works in GUI mode. */ | |
227 | 3785 if (!gui.in_use || gui.starting) |
7 | 3786 return; |
3787 | |
3788 /* Postpone the updating when more is to come. Speeds up executing of | |
3789 * mappings. */ | |
3790 if (shape_idx == -1 && char_avail()) | |
3791 { | |
3792 postponed_mouseshape = TRUE; | |
3793 return; | |
3794 } | |
3795 | |
864 | 3796 /* When ignoring the mouse don't change shape on the statusline. */ |
3797 if (*p_mouse == NUL | |
3798 && (shape_idx == SHAPE_IDX_CLINE | |
3799 || shape_idx == SHAPE_IDX_STATUS | |
3800 || shape_idx == SHAPE_IDX_VSEP)) | |
3801 shape_idx = -2; | |
3802 | |
7 | 3803 if (shape_idx == -2 |
3804 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape | |
3805 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape | |
3806 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape) | |
3807 return; | |
3808 if (shape_idx < 0) | |
3809 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape; | |
3810 else | |
3811 new_mouse_shape = shape_table[shape_idx].mshape; | |
3812 if (new_mouse_shape != old_mouse_shape) | |
3813 { | |
3814 mch_set_mouse_shape(new_mouse_shape); | |
3815 old_mouse_shape = new_mouse_shape; | |
3816 } | |
3817 postponed_mouseshape = FALSE; | |
3818 } | |
3819 # endif | |
3820 | |
3821 #endif /* CURSOR_SHAPE */ | |
3822 | |
3823 | |
3824 /* TODO: make some #ifdef for this */ | |
3825 /*--------[ file searching ]-------------------------------------------------*/ | |
3826 /* | |
3827 * File searching functions for 'path', 'tags' and 'cdpath' options. | |
3828 * External visible functions: | |
3829 * vim_findfile_init() creates/initialises the search context | |
3830 * vim_findfile_free_visited() free list of visited files/dirs of search | |
3831 * context | |
3832 * vim_findfile() find a file in the search context | |
3833 * vim_findfile_cleanup() cleanup/free search context created by | |
3834 * vim_findfile_init() | |
3835 * | |
3836 * All static functions and variables start with 'ff_' | |
3837 * | |
3838 * In general it works like this: | |
3839 * First you create yourself a search context by calling vim_findfile_init(). | |
3840 * It is possible to give a search context from a previous call to | |
3841 * vim_findfile_init(), so it can be reused. After this you call vim_findfile() | |
3842 * until you are satisfied with the result or it returns NULL. On every call it | |
3843 * returns the next file which matches the conditions given to | |
3844 * vim_findfile_init(). If it doesn't find a next file it returns NULL. | |
3845 * | |
3846 * It is possible to call vim_findfile_init() again to reinitialise your search | |
3847 * with some new parameters. Don't forget to pass your old search context to | |
3848 * it, so it can reuse it and especially reuse the list of already visited | |
3849 * directories. If you want to delete the list of already visited directories | |
3850 * simply call vim_findfile_free_visited(). | |
3851 * | |
3852 * When you are done call vim_findfile_cleanup() to free the search context. | |
3853 * | |
3854 * The function vim_findfile_init() has a long comment, which describes the | |
3855 * needed parameters. | |
3856 * | |
3857 * | |
3858 * | |
3859 * ATTENTION: | |
3860 * ========== | |
1072 | 3861 * Also we use an allocated search context here, this functions are NOT |
7 | 3862 * thread-safe!!!!! |
3863 * | |
3864 * To minimize parameter passing (or because I'm to lazy), only the | |
3865 * external visible functions get a search context as a parameter. This is | |
3866 * then assigned to a static global, which is used throughout the local | |
3867 * functions. | |
3868 */ | |
3869 | |
3870 /* | |
3871 * type for the directory search stack | |
3872 */ | |
3873 typedef struct ff_stack | |
3874 { | |
3875 struct ff_stack *ffs_prev; | |
3876 | |
3877 /* the fix part (no wildcards) and the part containing the wildcards | |
3878 * of the search path | |
3879 */ | |
3880 char_u *ffs_fix_path; | |
3881 #ifdef FEAT_PATH_EXTRA | |
3882 char_u *ffs_wc_path; | |
3883 #endif | |
3884 | |
3885 /* files/dirs found in the above directory, matched by the first wildcard | |
3886 * of wc_part | |
3887 */ | |
3888 char_u **ffs_filearray; | |
3889 int ffs_filearray_size; | |
3890 char_u ffs_filearray_cur; /* needed for partly handled dirs */ | |
3891 | |
3892 /* to store status of partly handled directories | |
1541 | 3893 * 0: we work on this directory for the first time |
7 | 3894 * 1: this directory was partly searched in an earlier step |
1541 | 3895 */ |
7 | 3896 int ffs_stage; |
3897 | |
3898 /* How deep are we in the directory tree? | |
3899 * Counts backward from value of level parameter to vim_findfile_init | |
3900 */ | |
3901 int ffs_level; | |
3902 | |
3903 /* Did we already expand '**' to an empty string? */ | |
3904 int ffs_star_star_empty; | |
3905 } ff_stack_T; | |
3906 | |
3907 /* | |
3908 * type for already visited directories or files. | |
3909 */ | |
3910 typedef struct ff_visited | |
3911 { | |
3912 struct ff_visited *ffv_next; | |
3913 | |
3914 #ifdef FEAT_PATH_EXTRA | |
3915 /* Visited directories are different if the wildcard string are | |
3916 * different. So we have to save it. | |
3917 */ | |
3918 char_u *ffv_wc_path; | |
3919 #endif | |
3920 /* for unix use inode etc for comparison (needed because of links), else | |
3921 * use filename. | |
3922 */ | |
3923 #ifdef UNIX | |
1881 | 3924 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */ |
3925 dev_t ffv_dev; /* device number */ | |
7 | 3926 ino_t ffv_ino; /* inode number */ |
3927 #endif | |
3928 /* The memory for this struct is allocated according to the length of | |
3929 * ffv_fname. | |
3930 */ | |
3931 char_u ffv_fname[1]; /* actually longer */ | |
3932 } ff_visited_T; | |
3933 | |
3934 /* | |
3935 * We might have to manage several visited lists during a search. | |
1209 | 3936 * This is especially needed for the tags option. If tags is set to: |
7 | 3937 * "./++/tags,./++/TAGS,++/tags" (replace + with *) |
3938 * So we have to do 3 searches: | |
3939 * 1) search from the current files directory downward for the file "tags" | |
3940 * 2) search from the current files directory downward for the file "TAGS" | |
3941 * 3) search from Vims current directory downwards for the file "tags" | |
3942 * As you can see, the first and the third search are for the same file, so for | |
3943 * the third search we can use the visited list of the first search. For the | |
3944 * second search we must start from a empty visited list. | |
3945 * The struct ff_visited_list_hdr is used to manage a linked list of already | |
3946 * visited lists. | |
3947 */ | |
3948 typedef struct ff_visited_list_hdr | |
3949 { | |
3950 struct ff_visited_list_hdr *ffvl_next; | |
3951 | |
3952 /* the filename the attached visited list is for */ | |
3953 char_u *ffvl_filename; | |
3954 | |
3955 ff_visited_T *ffvl_visited_list; | |
3956 | |
3957 } ff_visited_list_hdr_T; | |
3958 | |
3959 | |
3960 /* | |
3961 * '**' can be expanded to several directory levels. | |
1209 | 3962 * Set the default maximum depth. |
7 | 3963 */ |
3964 #define FF_MAX_STAR_STAR_EXPAND ((char_u)30) | |
1541 | 3965 |
7 | 3966 /* |
3967 * The search context: | |
3968 * ffsc_stack_ptr: the stack for the dirs to search | |
3969 * ffsc_visited_list: the currently active visited list | |
3970 * ffsc_dir_visited_list: the currently active visited list for search dirs | |
3971 * ffsc_visited_lists_list: the list of all visited lists | |
3972 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs | |
3973 * ffsc_file_to_search: the file to search for | |
3974 * ffsc_start_dir: the starting directory, if search path was relative | |
3975 * ffsc_fix_path: the fix part of the given path (without wildcards) | |
3976 * Needed for upward search. | |
3977 * ffsc_wc_path: the part of the given path containing wildcards | |
3978 * ffsc_level: how many levels of dirs to search downwards | |
3979 * ffsc_stopdirs_v: array of stop directories for upward search | |
1541 | 3980 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE |
2521
d890c820722f
Fix: 'suffixesadd' was used for finding tags file.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
3981 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd' |
7 | 3982 */ |
3983 typedef struct ff_search_ctx_T | |
3984 { | |
3985 ff_stack_T *ffsc_stack_ptr; | |
3986 ff_visited_list_hdr_T *ffsc_visited_list; | |
3987 ff_visited_list_hdr_T *ffsc_dir_visited_list; | |
3988 ff_visited_list_hdr_T *ffsc_visited_lists_list; | |
3989 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list; | |
3990 char_u *ffsc_file_to_search; | |
3991 char_u *ffsc_start_dir; | |
3992 char_u *ffsc_fix_path; | |
3993 #ifdef FEAT_PATH_EXTRA | |
3994 char_u *ffsc_wc_path; | |
3995 int ffsc_level; | |
3996 char_u **ffsc_stopdirs_v; | |
3997 #endif | |
1541 | 3998 int ffsc_find_what; |
2521
d890c820722f
Fix: 'suffixesadd' was used for finding tags file.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
3999 int ffsc_tagfile; |
359 | 4000 } ff_search_ctx_T; |
4001 | |
7 | 4002 /* locally needed functions */ |
4003 #ifdef FEAT_PATH_EXTRA | |
4004 static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *)); | |
4005 #else | |
4006 static int ff_check_visited __ARGS((ff_visited_T **, char_u *)); | |
4007 #endif | |
4008 static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp)); | |
4009 static void ff_free_visited_list __ARGS((ff_visited_T *vl)); | |
4010 static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp)); | |
4011 #ifdef FEAT_PATH_EXTRA | |
4012 static int ff_wc_equal __ARGS((char_u *s1, char_u *s2)); | |
4013 #endif | |
4014 | |
1541 | 4015 static void ff_push __ARGS((ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)); |
4016 static ff_stack_T *ff_pop __ARGS((ff_search_ctx_T *search_ctx)); | |
4017 static void ff_clear __ARGS((ff_search_ctx_T *search_ctx)); | |
4018 static void ff_free_stack_element __ARGS((ff_stack_T *stack_ptr)); | |
7 | 4019 #ifdef FEAT_PATH_EXTRA |
4020 static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int)); | |
4021 #else | |
4022 static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int)); | |
4023 #endif | |
4024 #ifdef FEAT_PATH_EXTRA | |
4025 static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **)); | |
4026 #endif | |
4027 | |
3202 | 4028 static char_u e_pathtoolong[] = N_("E854: path too long for completion"); |
4029 | |
7 | 4030 #if 0 |
4031 /* | |
4032 * if someone likes findfirst/findnext, here are the functions | |
4033 * NOT TESTED!! | |
4034 */ | |
4035 | |
4036 static void *ff_fn_search_context = NULL; | |
4037 | |
4038 char_u * | |
4039 vim_findfirst(path, filename, level) | |
4040 char_u *path; | |
4041 char_u *filename; | |
4042 int level; | |
4043 { | |
4044 ff_fn_search_context = | |
4045 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE, | |
4046 ff_fn_search_context, rel_fname); | |
4047 if (NULL == ff_fn_search_context) | |
4048 return NULL; | |
4049 else | |
4050 return vim_findnext() | |
4051 } | |
4052 | |
4053 char_u * | |
4054 vim_findnext() | |
4055 { | |
4056 char_u *ret = vim_findfile(ff_fn_search_context); | |
4057 | |
4058 if (NULL == ret) | |
4059 { | |
4060 vim_findfile_cleanup(ff_fn_search_context); | |
4061 ff_fn_search_context = NULL; | |
4062 } | |
4063 return ret; | |
4064 } | |
4065 #endif | |
4066 | |
4067 /* | |
2522
d7ecfc8b784c
Update help about wildcards in 'tags' option.
Bram Moolenaar <bram@vim.org>
parents:
2521
diff
changeset
|
4068 * Initialization routine for vim_findfile(). |
7 | 4069 * |
2211
dded7e33d0a9
Fix wrong memory access when clearing crypt key.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
4070 * Returns the newly allocated search context or NULL if an error occurred. |
7 | 4071 * |
4072 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done | |
4073 * with the search context. | |
4074 * | |
4075 * Find the file 'filename' in the directory 'path'. | |
4076 * The parameter 'path' may contain wildcards. If so only search 'level' | |
4077 * directories deep. The parameter 'level' is the absolute maximum and is | |
4078 * not related to restricts given to the '**' wildcard. If 'level' is 100 | |
4079 * and you use '**200' vim_findfile() will stop after 100 levels. | |
4080 * | |
1541 | 4081 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to |
4082 * escape special characters. | |
4083 * | |
7 | 4084 * If 'stopdirs' is not NULL and nothing is found downward, the search is |
4085 * restarted on the next higher directory level. This is repeated until the | |
4086 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the | |
4087 * format ";*<dirname>*\(;<dirname>\)*;\=$". | |
4088 * | |
4089 * If the 'path' is relative, the starting dir for the search is either VIM's | |
4090 * current dir or if the path starts with "./" the current files dir. | |
2211
dded7e33d0a9
Fix wrong memory access when clearing crypt key.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
4091 * If the 'path' is absolute, the starting dir is that part of the path before |
7 | 4092 * the first wildcard. |
4093 * | |
4094 * Upward search is only done on the starting dir. | |
4095 * | |
4096 * If 'free_visited' is TRUE the list of already visited files/directories is | |
4097 * cleared. Set this to FALSE if you just want to search from another | |
4098 * directory, but want to be sure that no directory from a previous search is | |
4099 * searched again. This is useful if you search for a file at different places. | |
4100 * The list of visited files/dirs can also be cleared with the function | |
4101 * vim_findfile_free_visited(). | |
4102 * | |
1541 | 4103 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for |
4104 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both. | |
7 | 4105 * |
4106 * A search context returned by a previous call to vim_findfile_init() can be | |
1541 | 4107 * passed in the parameter "search_ctx_arg". This context is reused and |
4108 * reinitialized with the new parameters. The list of already visited | |
7 | 4109 * directories from this context is only deleted if the parameter |
1541 | 4110 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed |
4111 * if the reinitialization fails. | |
7 | 4112 * |
1541 | 4113 * If you don't have a search context from a previous call "search_ctx_arg" |
4114 * must be NULL. | |
7 | 4115 * |
4116 * This function silently ignores a few errors, vim_findfile() will have | |
4117 * limited functionality then. | |
4118 */ | |
4119 void * | |
1541 | 4120 vim_findfile_init(path, filename, stopdirs, level, free_visited, find_what, |
4121 search_ctx_arg, tagfile, rel_fname) | |
7 | 4122 char_u *path; |
4123 char_u *filename; | |
1881 | 4124 char_u *stopdirs UNUSED; |
7 | 4125 int level; |
4126 int free_visited; | |
1541 | 4127 int find_what; |
4128 void *search_ctx_arg; | |
2521
d890c820722f
Fix: 'suffixesadd' was used for finding tags file.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
4129 int tagfile; /* expanding names of tags files */ |
7 | 4130 char_u *rel_fname; /* file name to use for "." */ |
4131 { | |
4132 #ifdef FEAT_PATH_EXTRA | |
1541 | 4133 char_u *wc_part; |
4134 #endif | |
4135 ff_stack_T *sptr; | |
4136 ff_search_ctx_T *search_ctx; | |
7 | 4137 |
4138 /* If a search context is given by the caller, reuse it, else allocate a | |
4139 * new one. | |
4140 */ | |
1541 | 4141 if (search_ctx_arg != NULL) |
4142 search_ctx = search_ctx_arg; | |
7 | 4143 else |
4144 { | |
1541 | 4145 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T)); |
4146 if (search_ctx == NULL) | |
7 | 4147 goto error_return; |
2215
cccb71c2c5c1
Fix uninit memory read in undo code. Fix uint32_t in proto file.
Bram Moolenaar <bram@vim.org>
parents:
2211
diff
changeset
|
4148 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T)); |
7 | 4149 } |
1541 | 4150 search_ctx->ffsc_find_what = find_what; |
2521
d890c820722f
Fix: 'suffixesadd' was used for finding tags file.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
4151 search_ctx->ffsc_tagfile = tagfile; |
7 | 4152 |
4153 /* clear the search context, but NOT the visited lists */ | |
1541 | 4154 ff_clear(search_ctx); |
7 | 4155 |
4156 /* clear visited list if wanted */ | |
4157 if (free_visited == TRUE) | |
1541 | 4158 vim_findfile_free_visited(search_ctx); |
7 | 4159 else |
4160 { | |
4161 /* Reuse old visited lists. Get the visited list for the given | |
4162 * filename. If no list for the current filename exists, creates a new | |
1541 | 4163 * one. */ |
4164 search_ctx->ffsc_visited_list = ff_get_visited_list(filename, | |
4165 &search_ctx->ffsc_visited_lists_list); | |
4166 if (search_ctx->ffsc_visited_list == NULL) | |
7 | 4167 goto error_return; |
1541 | 4168 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename, |
4169 &search_ctx->ffsc_dir_visited_lists_list); | |
4170 if (search_ctx->ffsc_dir_visited_list == NULL) | |
7 | 4171 goto error_return; |
4172 } | |
4173 | |
4174 if (ff_expand_buffer == NULL) | |
4175 { | |
4176 ff_expand_buffer = (char_u*)alloc(MAXPATHL); | |
4177 if (ff_expand_buffer == NULL) | |
4178 goto error_return; | |
4179 } | |
4180 | |
4181 /* Store information on starting dir now if path is relative. | |
20 | 4182 * If path is absolute, we do that later. */ |
7 | 4183 if (path[0] == '.' |
4184 && (vim_ispathsep(path[1]) || path[1] == NUL) | |
4185 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL) | |
4186 && rel_fname != NULL) | |
4187 { | |
4188 int len = (int)(gettail(rel_fname) - rel_fname); | |
4189 | |
4190 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL) | |
4191 { | |
4192 /* Make the start dir an absolute path name. */ | |
418 | 4193 vim_strncpy(ff_expand_buffer, rel_fname, len); |
1541 | 4194 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE); |
7 | 4195 } |
4196 else | |
1541 | 4197 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len); |
4198 if (search_ctx->ffsc_start_dir == NULL) | |
7 | 4199 goto error_return; |
4200 if (*++path != NUL) | |
4201 ++path; | |
4202 } | |
4203 else if (*path == NUL || !vim_isAbsName(path)) | |
4204 { | |
4205 #ifdef BACKSLASH_IN_FILENAME | |
4206 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */ | |
4207 if (*path != NUL && path[1] == ':') | |
4208 { | |
4209 char_u drive[3]; | |
4210 | |
4211 drive[0] = path[0]; | |
4212 drive[1] = ':'; | |
4213 drive[2] = NUL; | |
4214 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) | |
4215 goto error_return; | |
4216 path += 2; | |
4217 } | |
4218 else | |
4219 #endif | |
4220 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL) | |
4221 goto error_return; | |
4222 | |
1541 | 4223 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer); |
4224 if (search_ctx->ffsc_start_dir == NULL) | |
7 | 4225 goto error_return; |
4226 | |
4227 #ifdef BACKSLASH_IN_FILENAME | |
4228 /* A path that starts with "/dir" is relative to the drive, not to the | |
4229 * directory (but not for "//machine/dir"). Only use the drive name. */ | |
4230 if ((*path == '/' || *path == '\\') | |
4231 && path[1] != path[0] | |
1541 | 4232 && search_ctx->ffsc_start_dir[1] == ':') |
4233 search_ctx->ffsc_start_dir[2] = NUL; | |
7 | 4234 #endif |
4235 } | |
4236 | |
4237 #ifdef FEAT_PATH_EXTRA | |
4238 /* | |
4239 * If stopdirs are given, split them into an array of pointers. | |
4240 * If this fails (mem allocation), there is no upward search at all or a | |
4241 * stop directory is not recognized -> continue silently. | |
4242 * If stopdirs just contains a ";" or is empty, | |
1541 | 4243 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This |
7 | 4244 * is handled as unlimited upward search. See function |
4245 * ff_path_in_stoplist() for details. | |
4246 */ | |
4247 if (stopdirs != NULL) | |
4248 { | |
4249 char_u *walker = stopdirs; | |
4250 int dircount; | |
4251 | |
4252 while (*walker == ';') | |
4253 walker++; | |
4254 | |
4255 dircount = 1; | |
1541 | 4256 search_ctx->ffsc_stopdirs_v = |
4257 (char_u **)alloc((unsigned)sizeof(char_u *)); | |
4258 | |
4259 if (search_ctx->ffsc_stopdirs_v != NULL) | |
7 | 4260 { |
4261 do | |
4262 { | |
4263 char_u *helper; | |
4264 void *ptr; | |
4265 | |
4266 helper = walker; | |
1541 | 4267 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v, |
7 | 4268 (dircount + 1) * sizeof(char_u *)); |
4269 if (ptr) | |
1541 | 4270 search_ctx->ffsc_stopdirs_v = ptr; |
7 | 4271 else |
4272 /* ignore, keep what we have and continue */ | |
4273 break; | |
4274 walker = vim_strchr(walker, ';'); | |
4275 if (walker) | |
4276 { | |
1541 | 4277 search_ctx->ffsc_stopdirs_v[dircount-1] = |
4278 vim_strnsave(helper, (int)(walker - helper)); | |
7 | 4279 walker++; |
4280 } | |
4281 else | |
4282 /* this might be "", which means ascent till top | |
4283 * of directory tree. | |
4284 */ | |
1541 | 4285 search_ctx->ffsc_stopdirs_v[dircount-1] = |
4286 vim_strsave(helper); | |
7 | 4287 |
4288 dircount++; | |
4289 | |
4290 } while (walker != NULL); | |
1541 | 4291 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL; |
7 | 4292 } |
4293 } | |
4294 #endif | |
4295 | |
4296 #ifdef FEAT_PATH_EXTRA | |
1541 | 4297 search_ctx->ffsc_level = level; |
7 | 4298 |
4299 /* split into: | |
4300 * -fix path | |
4301 * -wildcard_stuff (might be NULL) | |
4302 */ | |
4303 wc_part = vim_strchr(path, '*'); | |
4304 if (wc_part != NULL) | |
4305 { | |
4306 int llevel; | |
4307 int len; | |
29 | 4308 char *errpt; |
7 | 4309 |
4310 /* save the fix part of the path */ | |
1541 | 4311 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path)); |
7 | 4312 |
4313 /* | |
4314 * copy wc_path and add restricts to the '**' wildcard. | |
1209 | 4315 * The octet after a '**' is used as a (binary) counter. |
7 | 4316 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3) |
4317 * or '**76' is transposed to '**N'( 'N' is ASCII value 76). | |
4318 * For EBCDIC you get different character values. | |
4319 * If no restrict is given after '**' the default is used. | |
1993 | 4320 * Due to this technique the path looks awful if you print it as a |
7 | 4321 * string. |
4322 */ | |
4323 len = 0; | |
4324 while (*wc_part != NUL) | |
4325 { | |
3202 | 4326 if (len + 5 >= MAXPATHL) |
4327 { | |
4328 EMSG(_(e_pathtoolong)); | |
4329 break; | |
4330 } | |
7 | 4331 if (STRNCMP(wc_part, "**", 2) == 0) |
4332 { | |
4333 ff_expand_buffer[len++] = *wc_part++; | |
4334 ff_expand_buffer[len++] = *wc_part++; | |
4335 | |
29 | 4336 llevel = strtol((char *)wc_part, &errpt, 10); |
4337 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255) | |
7 | 4338 ff_expand_buffer[len++] = llevel; |
29 | 4339 else if ((char_u *)errpt != wc_part && llevel == 0) |
7 | 4340 /* restrict is 0 -> remove already added '**' */ |
4341 len -= 2; | |
4342 else | |
4343 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND; | |
29 | 4344 wc_part = (char_u *)errpt; |
461 | 4345 if (*wc_part != NUL && !vim_ispathsep(*wc_part)) |
7 | 4346 { |
4347 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR); | |
4348 goto error_return; | |
4349 } | |
4350 } | |
4351 else | |
4352 ff_expand_buffer[len++] = *wc_part++; | |
4353 } | |
4354 ff_expand_buffer[len] = NUL; | |
1541 | 4355 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer); |
4356 | |
4357 if (search_ctx->ffsc_wc_path == NULL) | |
7 | 4358 goto error_return; |
4359 } | |
4360 else | |
4361 #endif | |
1541 | 4362 search_ctx->ffsc_fix_path = vim_strsave(path); |
4363 | |
4364 if (search_ctx->ffsc_start_dir == NULL) | |
7 | 4365 { |
4366 /* store the fix part as startdir. | |
4367 * This is needed if the parameter path is fully qualified. | |
4368 */ | |
1541 | 4369 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path); |
2445
04dae202d316
Fixes for coverity warnings.
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
4370 if (search_ctx->ffsc_start_dir == NULL) |
04dae202d316
Fixes for coverity warnings.
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
4371 goto error_return; |
04dae202d316
Fixes for coverity warnings.
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
4372 search_ctx->ffsc_fix_path[0] = NUL; |
7 | 4373 } |
4374 | |
4375 /* create an absolute path */ | |
3202 | 4376 if (STRLEN(search_ctx->ffsc_start_dir) |
4377 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL) | |
4378 { | |
4379 EMSG(_(e_pathtoolong)); | |
4380 goto error_return; | |
4381 } | |
1541 | 4382 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir); |
7 | 4383 add_pathsep(ff_expand_buffer); |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4384 { |
5124
6f24376028af
updated for version 7.3.1305
Bram Moolenaar <bram@vim.org>
parents:
5116
diff
changeset
|
4385 int eb_len = (int)STRLEN(ff_expand_buffer); |
6f24376028af
updated for version 7.3.1305
Bram Moolenaar <bram@vim.org>
parents:
5116
diff
changeset
|
4386 char_u *buf = alloc(eb_len |
6f24376028af
updated for version 7.3.1305
Bram Moolenaar <bram@vim.org>
parents:
5116
diff
changeset
|
4387 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1); |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4388 |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4389 STRCPY(buf, ff_expand_buffer); |
5110
dafd77a15d44
updated for version 7.3.1298
Bram Moolenaar <bram@vim.org>
parents:
5108
diff
changeset
|
4390 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path); |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4391 if (mch_isdir(buf)) |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4392 { |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4393 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path); |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4394 add_pathsep(ff_expand_buffer); |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4395 } |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4396 #ifdef FEAT_PATH_EXTRA |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4397 else |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4398 { |
5116
6cabac58f26f
updated for version 7.3.1301
Bram Moolenaar <bram@vim.org>
parents:
5110
diff
changeset
|
4399 char_u *p = gettail(search_ctx->ffsc_fix_path); |
5571 | 4400 char_u *wc_path = NULL; |
4401 char_u *temp = NULL; | |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4402 int len = 0; |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4403 |
5116
6cabac58f26f
updated for version 7.3.1301
Bram Moolenaar <bram@vim.org>
parents:
5110
diff
changeset
|
4404 if (p > search_ctx->ffsc_fix_path) |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4405 { |
5124
6f24376028af
updated for version 7.3.1305
Bram Moolenaar <bram@vim.org>
parents:
5116
diff
changeset
|
4406 len = (int)(p - search_ctx->ffsc_fix_path) - 1; |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4407 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len); |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4408 add_pathsep(ff_expand_buffer); |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4409 } |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4410 else |
5124
6f24376028af
updated for version 7.3.1305
Bram Moolenaar <bram@vim.org>
parents:
5116
diff
changeset
|
4411 len = (int)STRLEN(search_ctx->ffsc_fix_path); |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4412 |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4413 if (search_ctx->ffsc_wc_path != NULL) |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4414 { |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4415 wc_path = vim_strsave(search_ctx->ffsc_wc_path); |
5124
6f24376028af
updated for version 7.3.1305
Bram Moolenaar <bram@vim.org>
parents:
5116
diff
changeset
|
4416 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path) |
5151
d0288faf3086
updated for version 7.4a.002
Bram Moolenaar <bram@vim.org>
parents:
5124
diff
changeset
|
4417 + STRLEN(search_ctx->ffsc_fix_path + len) |
d0288faf3086
updated for version 7.4a.002
Bram Moolenaar <bram@vim.org>
parents:
5124
diff
changeset
|
4418 + 1)); |
7148
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4419 if (temp == NULL || wc_path == NULL) |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4420 { |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4421 vim_free(buf); |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4422 vim_free(temp); |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4423 vim_free(wc_path); |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4424 goto error_return; |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4425 } |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4426 |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4427 STRCPY(temp, search_ctx->ffsc_fix_path + len); |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4428 STRCAT(temp, search_ctx->ffsc_wc_path); |
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4429 vim_free(search_ctx->ffsc_wc_path); |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4430 vim_free(wc_path); |
7148
339e657a6ed6
commit https://github.com/vim/vim/commit/c79a5452acd695238798947e40086f9823c400e7
Christian Brabandt <cb@256bit.org>
parents:
7131
diff
changeset
|
4431 search_ctx->ffsc_wc_path = temp; |
5108
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4432 } |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4433 } |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4434 #endif |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4435 vim_free(buf); |
cb0a5c9c0f9b
updated for version 7.3.1297
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
4436 } |
7 | 4437 |
4438 sptr = ff_create_stack_element(ff_expand_buffer, | |
4439 #ifdef FEAT_PATH_EXTRA | |
1541 | 4440 search_ctx->ffsc_wc_path, |
7 | 4441 #endif |
4442 level, 0); | |
4443 | |
4444 if (sptr == NULL) | |
4445 goto error_return; | |
4446 | |
1541 | 4447 ff_push(search_ctx, sptr); |
4448 | |
4449 search_ctx->ffsc_file_to_search = vim_strsave(filename); | |
4450 if (search_ctx->ffsc_file_to_search == NULL) | |
7 | 4451 goto error_return; |
4452 | |
1541 | 4453 return search_ctx; |
7 | 4454 |
4455 error_return: | |
4456 /* | |
4457 * We clear the search context now! | |
4458 * Even when the caller gave us a (perhaps valid) context we free it here, | |
4459 * as we might have already destroyed it. | |
4460 */ | |
1541 | 4461 vim_findfile_cleanup(search_ctx); |
7 | 4462 return NULL; |
4463 } | |
4464 | |
4465 #if defined(FEAT_PATH_EXTRA) || defined(PROTO) | |
4466 /* | |
4467 * Get the stopdir string. Check that ';' is not escaped. | |
4468 */ | |
4469 char_u * | |
4470 vim_findfile_stopdir(buf) | |
4471 char_u *buf; | |
4472 { | |
4473 char_u *r_ptr = buf; | |
4474 | |
4475 while (*r_ptr != NUL && *r_ptr != ';') | |
4476 { | |
4477 if (r_ptr[0] == '\\' && r_ptr[1] == ';') | |
4478 { | |
2984 | 4479 /* Overwrite the escape char, |
4480 * use STRLEN(r_ptr) to move the trailing '\0'. */ | |
1621 | 4481 STRMOVE(r_ptr, r_ptr + 1); |
7 | 4482 r_ptr++; |
4483 } | |
4484 r_ptr++; | |
4485 } | |
4486 if (*r_ptr == ';') | |
4487 { | |
4488 *r_ptr = 0; | |
4489 r_ptr++; | |
4490 } | |
4491 else if (*r_ptr == NUL) | |
4492 r_ptr = NULL; | |
4493 return r_ptr; | |
4494 } | |
4495 #endif | |
4496 | |
1541 | 4497 /* |
4498 * Clean up the given search context. Can handle a NULL pointer. | |
4499 */ | |
7 | 4500 void |
4501 vim_findfile_cleanup(ctx) | |
4502 void *ctx; | |
4503 { | |
359 | 4504 if (ctx == NULL) |
7 | 4505 return; |
4506 | |
4507 vim_findfile_free_visited(ctx); | |
1541 | 4508 ff_clear(ctx); |
7 | 4509 vim_free(ctx); |
4510 } | |
4511 | |
4512 /* | |
4513 * Find a file in a search context. | |
4514 * The search context was created with vim_findfile_init() above. | |
4515 * Return a pointer to an allocated file name or NULL if nothing found. | |
4516 * To get all matching files call this function until you get NULL. | |
4517 * | |
20 | 4518 * If the passed search_context is NULL, NULL is returned. |
7 | 4519 * |
4520 * The search algorithm is depth first. To change this replace the | |
4521 * stack with a list (don't forget to leave partly searched directories on the | |
4522 * top of the list). | |
4523 */ | |
4524 char_u * | |
1541 | 4525 vim_findfile(search_ctx_arg) |
4526 void *search_ctx_arg; | |
7 | 4527 { |
4528 char_u *file_path; | |
4529 #ifdef FEAT_PATH_EXTRA | |
4530 char_u *rest_of_wildcards; | |
4531 char_u *path_end = NULL; | |
4532 #endif | |
1541 | 4533 ff_stack_T *stackp; |
7 | 4534 #if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA) |
4535 int len; | |
4536 #endif | |
4537 int i; | |
4538 char_u *p; | |
4539 #ifdef FEAT_SEARCHPATH | |
4540 char_u *suf; | |
4541 #endif | |
1541 | 4542 ff_search_ctx_T *search_ctx; |
4543 | |
4544 if (search_ctx_arg == NULL) | |
7 | 4545 return NULL; |
4546 | |
1541 | 4547 search_ctx = (ff_search_ctx_T *)search_ctx_arg; |
7 | 4548 |
4549 /* | |
4550 * filepath is used as buffer for various actions and as the storage to | |
4551 * return a found filename. | |
4552 */ | |
4553 if ((file_path = alloc((int)MAXPATHL)) == NULL) | |
4554 return NULL; | |
4555 | |
4556 #ifdef FEAT_PATH_EXTRA | |
4557 /* store the end of the start dir -- needed for upward search */ | |
1541 | 4558 if (search_ctx->ffsc_start_dir != NULL) |
4559 path_end = &search_ctx->ffsc_start_dir[ | |
4560 STRLEN(search_ctx->ffsc_start_dir)]; | |
7 | 4561 #endif |
4562 | |
4563 #ifdef FEAT_PATH_EXTRA | |
4564 /* upward search loop */ | |
4565 for (;;) | |
4566 { | |
4567 #endif | |
4568 /* downward search loop */ | |
4569 for (;;) | |
4570 { | |
4571 /* check if user user wants to stop the search*/ | |
4572 ui_breakcheck(); | |
4573 if (got_int) | |
4574 break; | |
4575 | |
4576 /* get directory to work on from stack */ | |
1541 | 4577 stackp = ff_pop(search_ctx); |
4578 if (stackp == NULL) | |
7 | 4579 break; |
4580 | |
4581 /* | |
4582 * TODO: decide if we leave this test in | |
4583 * | |
4584 * GOOD: don't search a directory(-tree) twice. | |
4585 * BAD: - check linked list for every new directory entered. | |
4586 * - check for double files also done below | |
4587 * | |
4588 * Here we check if we already searched this directory. | |
4589 * We already searched a directory if: | |
4590 * 1) The directory is the same. | |
4591 * 2) We would use the same wildcard string. | |
4592 * | |
4593 * Good if you have links on same directory via several ways | |
4594 * or you have selfreferences in directories (e.g. SuSE Linux 6.3: | |
4595 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop) | |
4596 * | |
4597 * This check is only needed for directories we work on for the | |
1541 | 4598 * first time (hence stackp->ff_filearray == NULL) |
7 | 4599 */ |
1541 | 4600 if (stackp->ffs_filearray == NULL |
4601 && ff_check_visited(&search_ctx->ffsc_dir_visited_list | |
7 | 4602 ->ffvl_visited_list, |
1541 | 4603 stackp->ffs_fix_path |
7 | 4604 #ifdef FEAT_PATH_EXTRA |
1541 | 4605 , stackp->ffs_wc_path |
7 | 4606 #endif |
4607 ) == FAIL) | |
4608 { | |
4609 #ifdef FF_VERBOSE | |
4610 if (p_verbose >= 5) | |
4611 { | |
293 | 4612 verbose_enter_scroll(); |
7 | 4613 smsg((char_u *)"Already Searched: %s (%s)", |
1541 | 4614 stackp->ffs_fix_path, stackp->ffs_wc_path); |
7 | 4615 /* don't overwrite this either */ |
4616 msg_puts((char_u *)"\n"); | |
293 | 4617 verbose_leave_scroll(); |
7 | 4618 } |
4619 #endif | |
1541 | 4620 ff_free_stack_element(stackp); |
7 | 4621 continue; |
4622 } | |
4623 #ifdef FF_VERBOSE | |
4624 else if (p_verbose >= 5) | |
4625 { | |
293 | 4626 verbose_enter_scroll(); |
273 | 4627 smsg((char_u *)"Searching: %s (%s)", |
1541 | 4628 stackp->ffs_fix_path, stackp->ffs_wc_path); |
7 | 4629 /* don't overwrite this either */ |
4630 msg_puts((char_u *)"\n"); | |
293 | 4631 verbose_leave_scroll(); |
7 | 4632 } |
4633 #endif | |
4634 | |
4635 /* check depth */ | |
1541 | 4636 if (stackp->ffs_level <= 0) |
7 | 4637 { |
1541 | 4638 ff_free_stack_element(stackp); |
7 | 4639 continue; |
4640 } | |
4641 | |
4642 file_path[0] = NUL; | |
4643 | |
4644 /* | |
4645 * If no filearray till now expand wildcards | |
4646 * The function expand_wildcards() can handle an array of paths | |
4647 * and all possible expands are returned in one array. We use this | |
4648 * to handle the expansion of '**' into an empty string. | |
4649 */ | |
1541 | 4650 if (stackp->ffs_filearray == NULL) |
7 | 4651 { |
4652 char_u *dirptrs[2]; | |
4653 | |
4654 /* we use filepath to build the path expand_wildcards() should | |
4655 * expand. | |
4656 */ | |
4657 dirptrs[0] = file_path; | |
4658 dirptrs[1] = NULL; | |
4659 | |
4660 /* if we have a start dir copy it in */ | |
1541 | 4661 if (!vim_isAbsName(stackp->ffs_fix_path) |
4662 && search_ctx->ffsc_start_dir) | |
7 | 4663 { |
1541 | 4664 STRCPY(file_path, search_ctx->ffsc_start_dir); |
7 | 4665 add_pathsep(file_path); |
4666 } | |
4667 | |
4668 /* append the fix part of the search path */ | |
1541 | 4669 STRCAT(file_path, stackp->ffs_fix_path); |
7 | 4670 add_pathsep(file_path); |
4671 | |
4672 #ifdef FEAT_PATH_EXTRA | |
1541 | 4673 rest_of_wildcards = stackp->ffs_wc_path; |
7 | 4674 if (*rest_of_wildcards != NUL) |
4675 { | |
4676 len = (int)STRLEN(file_path); | |
4677 if (STRNCMP(rest_of_wildcards, "**", 2) == 0) | |
4678 { | |
4679 /* pointer to the restrict byte | |
4680 * The restrict byte is not a character! | |
4681 */ | |
4682 p = rest_of_wildcards + 2; | |
4683 | |
4684 if (*p > 0) | |
4685 { | |
4686 (*p)--; | |
4687 file_path[len++] = '*'; | |
4688 } | |
4689 | |
4690 if (*p == 0) | |
4691 { | |
4692 /* remove '**<numb> from wildcards */ | |
1621 | 4693 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3); |
7 | 4694 } |
4695 else | |
4696 rest_of_wildcards += 3; | |
4697 | |
1541 | 4698 if (stackp->ffs_star_star_empty == 0) |
7 | 4699 { |
4700 /* if not done before, expand '**' to empty */ | |
1541 | 4701 stackp->ffs_star_star_empty = 1; |
4702 dirptrs[1] = stackp->ffs_fix_path; | |
7 | 4703 } |
4704 } | |
4705 | |
4706 /* | |
4707 * Here we copy until the next path separator or the end of | |
4708 * the path. If we stop at a path separator, there is | |
1209 | 4709 * still something else left. This is handled below by |
7 | 4710 * pushing every directory returned from expand_wildcards() |
4711 * on the stack again for further search. | |
4712 */ | |
4713 while (*rest_of_wildcards | |
4714 && !vim_ispathsep(*rest_of_wildcards)) | |
4715 file_path[len++] = *rest_of_wildcards++; | |
4716 | |
4717 file_path[len] = NUL; | |
4718 if (vim_ispathsep(*rest_of_wildcards)) | |
4719 rest_of_wildcards++; | |
4720 } | |
4721 #endif | |
4722 | |
4723 /* | |
4724 * Expand wildcards like "*" and "$VAR". | |
4725 * If the path is a URL don't try this. | |
4726 */ | |
4727 if (path_with_url(dirptrs[0])) | |
4728 { | |
1541 | 4729 stackp->ffs_filearray = (char_u **) |
7 | 4730 alloc((unsigned)sizeof(char *)); |
1541 | 4731 if (stackp->ffs_filearray != NULL |
4732 && (stackp->ffs_filearray[0] | |
7 | 4733 = vim_strsave(dirptrs[0])) != NULL) |
1541 | 4734 stackp->ffs_filearray_size = 1; |
7 | 4735 else |
1541 | 4736 stackp->ffs_filearray_size = 0; |
7 | 4737 } |
4738 else | |
2984 | 4739 /* Add EW_NOTWILD because the expanded path may contain |
4740 * wildcard characters that are to be taken literally. | |
4741 * This is a bit of a hack. */ | |
7 | 4742 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs, |
1541 | 4743 &stackp->ffs_filearray_size, |
4744 &stackp->ffs_filearray, | |
2984 | 4745 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD); |
7 | 4746 |
1541 | 4747 stackp->ffs_filearray_cur = 0; |
4748 stackp->ffs_stage = 0; | |
7 | 4749 } |
4750 #ifdef FEAT_PATH_EXTRA | |
4751 else | |
1541 | 4752 rest_of_wildcards = &stackp->ffs_wc_path[ |
4753 STRLEN(stackp->ffs_wc_path)]; | |
4754 #endif | |
4755 | |
4756 if (stackp->ffs_stage == 0) | |
7 | 4757 { |
4758 /* this is the first time we work on this directory */ | |
4759 #ifdef FEAT_PATH_EXTRA | |
4760 if (*rest_of_wildcards == NUL) | |
4761 #endif | |
4762 { | |
4763 /* | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
4764 * We don't have further wildcards to expand, so we have to |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
4765 * check for the final file now. |
7 | 4766 */ |
1541 | 4767 for (i = stackp->ffs_filearray_cur; |
4768 i < stackp->ffs_filearray_size; ++i) | |
7 | 4769 { |
1541 | 4770 if (!path_with_url(stackp->ffs_filearray[i]) |
4771 && !mch_isdir(stackp->ffs_filearray[i])) | |
7 | 4772 continue; /* not a directory */ |
4773 | |
1993 | 4774 /* prepare the filename to be checked for existence |
7 | 4775 * below */ |
1541 | 4776 STRCPY(file_path, stackp->ffs_filearray[i]); |
7 | 4777 add_pathsep(file_path); |
1541 | 4778 STRCAT(file_path, search_ctx->ffsc_file_to_search); |
7 | 4779 |
4780 /* | |
4781 * Try without extra suffix and then with suffixes | |
4782 * from 'suffixesadd'. | |
4783 */ | |
4784 #ifdef FEAT_SEARCHPATH | |
4785 len = (int)STRLEN(file_path); | |
2521
d890c820722f
Fix: 'suffixesadd' was used for finding tags file.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
4786 if (search_ctx->ffsc_tagfile) |
d890c820722f
Fix: 'suffixesadd' was used for finding tags file.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
4787 suf = (char_u *)""; |
d890c820722f
Fix: 'suffixesadd' was used for finding tags file.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
4788 else |
d890c820722f
Fix: 'suffixesadd' was used for finding tags file.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
4789 suf = curbuf->b_p_sua; |
7 | 4790 for (;;) |
4791 #endif | |
4792 { | |
4793 /* if file exists and we didn't already find it */ | |
4794 if ((path_with_url(file_path) | |
1541 | 4795 || (mch_getperm(file_path) >= 0 |
4796 && (search_ctx->ffsc_find_what | |
4797 == FINDFILE_BOTH | |
4798 || ((search_ctx->ffsc_find_what | |
4799 == FINDFILE_DIR) | |
4800 == mch_isdir(file_path))))) | |
7 | 4801 #ifndef FF_VERBOSE |
4802 && (ff_check_visited( | |
1541 | 4803 &search_ctx->ffsc_visited_list->ffvl_visited_list, |
7 | 4804 file_path |
4805 #ifdef FEAT_PATH_EXTRA | |
4806 , (char_u *)"" | |
4807 #endif | |
4808 ) == OK) | |
4809 #endif | |
4810 ) | |
4811 { | |
4812 #ifdef FF_VERBOSE | |
4813 if (ff_check_visited( | |
1541 | 4814 &search_ctx->ffsc_visited_list->ffvl_visited_list, |
7 | 4815 file_path |
4816 #ifdef FEAT_PATH_EXTRA | |
4817 , (char_u *)"" | |
4818 #endif | |
4819 ) == FAIL) | |
4820 { | |
4821 if (p_verbose >= 5) | |
4822 { | |
293 | 4823 verbose_enter_scroll(); |
273 | 4824 smsg((char_u *)"Already: %s", |
7 | 4825 file_path); |
4826 /* don't overwrite this either */ | |
4827 msg_puts((char_u *)"\n"); | |
293 | 4828 verbose_leave_scroll(); |
7 | 4829 } |
4830 continue; | |
4831 } | |
4832 #endif | |
4833 | |
4834 /* push dir to examine rest of subdirs later */ | |
1541 | 4835 stackp->ffs_filearray_cur = i + 1; |
4836 ff_push(search_ctx, stackp); | |
7 | 4837 |
1789 | 4838 if (!path_with_url(file_path)) |
4839 simplify_filename(file_path); | |
7 | 4840 if (mch_dirname(ff_expand_buffer, MAXPATHL) |
4841 == OK) | |
4842 { | |
4843 p = shorten_fname(file_path, | |
4844 ff_expand_buffer); | |
4845 if (p != NULL) | |
1621 | 4846 STRMOVE(file_path, p); |
7 | 4847 } |
4848 #ifdef FF_VERBOSE | |
4849 if (p_verbose >= 5) | |
4850 { | |
293 | 4851 verbose_enter_scroll(); |
273 | 4852 smsg((char_u *)"HIT: %s", file_path); |
7 | 4853 /* don't overwrite this either */ |
4854 msg_puts((char_u *)"\n"); | |
293 | 4855 verbose_leave_scroll(); |
7 | 4856 } |
4857 #endif | |
4858 return file_path; | |
4859 } | |
4860 | |
4861 #ifdef FEAT_SEARCHPATH | |
4862 /* Not found or found already, try next suffix. */ | |
4863 if (*suf == NUL) | |
4864 break; | |
4865 copy_option_part(&suf, file_path + len, | |
4866 MAXPATHL - len, ","); | |
4867 #endif | |
4868 } | |
4869 } | |
4870 } | |
4871 #ifdef FEAT_PATH_EXTRA | |
4872 else | |
4873 { | |
4874 /* | |
4875 * still wildcards left, push the directories for further | |
4876 * search | |
4877 */ | |
1541 | 4878 for (i = stackp->ffs_filearray_cur; |
4879 i < stackp->ffs_filearray_size; ++i) | |
7 | 4880 { |
1541 | 4881 if (!mch_isdir(stackp->ffs_filearray[i])) |
7 | 4882 continue; /* not a directory */ |
4883 | |
1541 | 4884 ff_push(search_ctx, |
4885 ff_create_stack_element( | |
4886 stackp->ffs_filearray[i], | |
4887 rest_of_wildcards, | |
4888 stackp->ffs_level - 1, 0)); | |
7 | 4889 } |
4890 } | |
4891 #endif | |
1541 | 4892 stackp->ffs_filearray_cur = 0; |
4893 stackp->ffs_stage = 1; | |
7 | 4894 } |
4895 | |
4896 #ifdef FEAT_PATH_EXTRA | |
4897 /* | |
4898 * if wildcards contains '**' we have to descent till we reach the | |
4899 * leaves of the directory tree. | |
4900 */ | |
1541 | 4901 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0) |
7 | 4902 { |
1541 | 4903 for (i = stackp->ffs_filearray_cur; |
4904 i < stackp->ffs_filearray_size; ++i) | |
7 | 4905 { |
1541 | 4906 if (fnamecmp(stackp->ffs_filearray[i], |
4907 stackp->ffs_fix_path) == 0) | |
7 | 4908 continue; /* don't repush same directory */ |
1541 | 4909 if (!mch_isdir(stackp->ffs_filearray[i])) |
7 | 4910 continue; /* not a directory */ |
1541 | 4911 ff_push(search_ctx, |
4912 ff_create_stack_element(stackp->ffs_filearray[i], | |
4913 stackp->ffs_wc_path, stackp->ffs_level - 1, 1)); | |
7 | 4914 } |
4915 } | |
4916 #endif | |
4917 | |
4918 /* we are done with the current directory */ | |
1541 | 4919 ff_free_stack_element(stackp); |
7 | 4920 |
4921 } | |
4922 | |
4923 #ifdef FEAT_PATH_EXTRA | |
4924 /* If we reached this, we didn't find anything downwards. | |
4925 * Let's check if we should do an upward search. | |
4926 */ | |
1541 | 4927 if (search_ctx->ffsc_start_dir |
4928 && search_ctx->ffsc_stopdirs_v != NULL && !got_int) | |
7 | 4929 { |
4930 ff_stack_T *sptr; | |
4931 | |
4932 /* is the last starting directory in the stop list? */ | |
1541 | 4933 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir, |
4934 (int)(path_end - search_ctx->ffsc_start_dir), | |
4935 search_ctx->ffsc_stopdirs_v) == TRUE) | |
7 | 4936 break; |
4937 | |
4938 /* cut of last dir */ | |
1541 | 4939 while (path_end > search_ctx->ffsc_start_dir |
4940 && vim_ispathsep(*path_end)) | |
7 | 4941 path_end--; |
1541 | 4942 while (path_end > search_ctx->ffsc_start_dir |
4943 && !vim_ispathsep(path_end[-1])) | |
7 | 4944 path_end--; |
4945 *path_end = 0; | |
4946 path_end--; | |
4947 | |
1541 | 4948 if (*search_ctx->ffsc_start_dir == 0) |
7 | 4949 break; |
4950 | |
1541 | 4951 STRCPY(file_path, search_ctx->ffsc_start_dir); |
7 | 4952 add_pathsep(file_path); |
1541 | 4953 STRCAT(file_path, search_ctx->ffsc_fix_path); |
7 | 4954 |
4955 /* create a new stack entry */ | |
4956 sptr = ff_create_stack_element(file_path, | |
1541 | 4957 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0); |
7 | 4958 if (sptr == NULL) |
4959 break; | |
1541 | 4960 ff_push(search_ctx, sptr); |
7 | 4961 } |
4962 else | |
4963 break; | |
4964 } | |
4965 #endif | |
4966 | |
4967 vim_free(file_path); | |
4968 return NULL; | |
4969 } | |
4970 | |
4971 /* | |
4972 * Free the list of lists of visited files and directories | |
4973 * Can handle it if the passed search_context is NULL; | |
4974 */ | |
4975 void | |
1541 | 4976 vim_findfile_free_visited(search_ctx_arg) |
4977 void *search_ctx_arg; | |
7 | 4978 { |
1541 | 4979 ff_search_ctx_T *search_ctx; |
4980 | |
4981 if (search_ctx_arg == NULL) | |
7 | 4982 return; |
4983 | |
1541 | 4984 search_ctx = (ff_search_ctx_T *)search_ctx_arg; |
4985 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list); | |
4986 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list); | |
7 | 4987 } |
4988 | |
4989 static void | |
4990 vim_findfile_free_visited_list(list_headp) | |
4991 ff_visited_list_hdr_T **list_headp; | |
4992 { | |
4993 ff_visited_list_hdr_T *vp; | |
4994 | |
4995 while (*list_headp != NULL) | |
4996 { | |
4997 vp = (*list_headp)->ffvl_next; | |
4998 ff_free_visited_list((*list_headp)->ffvl_visited_list); | |
4999 | |
5000 vim_free((*list_headp)->ffvl_filename); | |
5001 vim_free(*list_headp); | |
5002 *list_headp = vp; | |
5003 } | |
5004 *list_headp = NULL; | |
5005 } | |
5006 | |
5007 static void | |
5008 ff_free_visited_list(vl) | |
5009 ff_visited_T *vl; | |
5010 { | |
5011 ff_visited_T *vp; | |
5012 | |
5013 while (vl != NULL) | |
5014 { | |
5015 vp = vl->ffv_next; | |
5016 #ifdef FEAT_PATH_EXTRA | |
5017 vim_free(vl->ffv_wc_path); | |
5018 #endif | |
5019 vim_free(vl); | |
5020 vl = vp; | |
5021 } | |
5022 vl = NULL; | |
5023 } | |
5024 | |
5025 /* | |
5026 * Returns the already visited list for the given filename. If none is found it | |
5027 * allocates a new one. | |
5028 */ | |
5029 static ff_visited_list_hdr_T* | |
5030 ff_get_visited_list(filename, list_headp) | |
5031 char_u *filename; | |
5032 ff_visited_list_hdr_T **list_headp; | |
5033 { | |
5034 ff_visited_list_hdr_T *retptr = NULL; | |
5035 | |
5036 /* check if a visited list for the given filename exists */ | |
5037 if (*list_headp != NULL) | |
5038 { | |
5039 retptr = *list_headp; | |
5040 while (retptr != NULL) | |
5041 { | |
5042 if (fnamecmp(filename, retptr->ffvl_filename) == 0) | |
5043 { | |
5044 #ifdef FF_VERBOSE | |
5045 if (p_verbose >= 5) | |
5046 { | |
293 | 5047 verbose_enter_scroll(); |
273 | 5048 smsg((char_u *)"ff_get_visited_list: FOUND list for %s", |
7 | 5049 filename); |
5050 /* don't overwrite this either */ | |
5051 msg_puts((char_u *)"\n"); | |
293 | 5052 verbose_leave_scroll(); |
7 | 5053 } |
5054 #endif | |
5055 return retptr; | |
5056 } | |
5057 retptr = retptr->ffvl_next; | |
5058 } | |
5059 } | |
5060 | |
5061 #ifdef FF_VERBOSE | |
5062 if (p_verbose >= 5) | |
5063 { | |
293 | 5064 verbose_enter_scroll(); |
273 | 5065 smsg((char_u *)"ff_get_visited_list: new list for %s", filename); |
7 | 5066 /* don't overwrite this either */ |
5067 msg_puts((char_u *)"\n"); | |
293 | 5068 verbose_leave_scroll(); |
7 | 5069 } |
5070 #endif | |
5071 | |
5072 /* | |
5073 * if we reach this we didn't find a list and we have to allocate new list | |
5074 */ | |
5075 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr)); | |
5076 if (retptr == NULL) | |
5077 return NULL; | |
5078 | |
5079 retptr->ffvl_visited_list = NULL; | |
5080 retptr->ffvl_filename = vim_strsave(filename); | |
5081 if (retptr->ffvl_filename == NULL) | |
5082 { | |
5083 vim_free(retptr); | |
5084 return NULL; | |
5085 } | |
5086 retptr->ffvl_next = *list_headp; | |
5087 *list_headp = retptr; | |
5088 | |
5089 return retptr; | |
5090 } | |
5091 | |
5092 #ifdef FEAT_PATH_EXTRA | |
5093 /* | |
5094 * check if two wildcard paths are equal. Returns TRUE or FALSE. | |
5095 * They are equal if: | |
5096 * - both paths are NULL | |
5097 * - they have the same length | |
5098 * - char by char comparison is OK | |
5099 * - the only differences are in the counters behind a '**', so | |
5100 * '**\20' is equal to '**\24' | |
5101 */ | |
5102 static int | |
5103 ff_wc_equal(s1, s2) | |
5104 char_u *s1; | |
5105 char_u *s2; | |
5106 { | |
7044
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5107 int i, j; |
7062
6deb9d802fe4
commit https://github.com/vim/vim/commit/d43f0951bca162d4491d57df9277b5dbc462944f
Christian Brabandt <cb@256bit.org>
parents:
7044
diff
changeset
|
5108 int c1 = NUL; |
6deb9d802fe4
commit https://github.com/vim/vim/commit/d43f0951bca162d4491d57df9277b5dbc462944f
Christian Brabandt <cb@256bit.org>
parents:
7044
diff
changeset
|
5109 int c2 = NUL; |
4246 | 5110 int prev1 = NUL; |
5111 int prev2 = NUL; | |
7 | 5112 |
5113 if (s1 == s2) | |
5114 return TRUE; | |
5115 | |
5116 if (s1 == NULL || s2 == NULL) | |
5117 return FALSE; | |
5118 | |
7062
6deb9d802fe4
commit https://github.com/vim/vim/commit/d43f0951bca162d4491d57df9277b5dbc462944f
Christian Brabandt <cb@256bit.org>
parents:
7044
diff
changeset
|
5119 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;) |
7 | 5120 { |
7062
6deb9d802fe4
commit https://github.com/vim/vim/commit/d43f0951bca162d4491d57df9277b5dbc462944f
Christian Brabandt <cb@256bit.org>
parents:
7044
diff
changeset
|
5121 c1 = PTR2CHAR(s1 + i); |
6deb9d802fe4
commit https://github.com/vim/vim/commit/d43f0951bca162d4491d57df9277b5dbc462944f
Christian Brabandt <cb@256bit.org>
parents:
7044
diff
changeset
|
5122 c2 = PTR2CHAR(s2 + j); |
4246 | 5123 |
5124 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2) | |
5125 && (prev1 != '*' || prev2 != '*')) | |
7062
6deb9d802fe4
commit https://github.com/vim/vim/commit/d43f0951bca162d4491d57df9277b5dbc462944f
Christian Brabandt <cb@256bit.org>
parents:
7044
diff
changeset
|
5126 return FALSE; |
4246 | 5127 prev2 = prev1; |
5128 prev1 = c1; | |
7044
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5129 |
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5130 i += MB_PTR2LEN(s1 + i); |
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5131 j += MB_PTR2LEN(s2 + j); |
7 | 5132 } |
7131
cc5570ed684e
commit https://github.com/vim/vim/commit/4d0c7bc74ac6fad5cb599dc3ade6996e848d83b6
Christian Brabandt <cb@256bit.org>
parents:
7111
diff
changeset
|
5133 return s1[i] == s2[j]; |
7 | 5134 } |
5135 #endif | |
5136 | |
5137 /* | |
5138 * maintains the list of already visited files and dirs | |
5139 * returns FAIL if the given file/dir is already in the list | |
5140 * returns OK if it is newly added | |
5141 * | |
5142 * TODO: What to do on memory allocation problems? | |
5143 * -> return TRUE - Better the file is found several times instead of | |
5144 * never. | |
5145 */ | |
5146 static int | |
5147 ff_check_visited(visited_list, fname | |
5148 #ifdef FEAT_PATH_EXTRA | |
5149 , wc_path | |
5150 #endif | |
5151 ) | |
5152 ff_visited_T **visited_list; | |
5153 char_u *fname; | |
5154 #ifdef FEAT_PATH_EXTRA | |
5155 char_u *wc_path; | |
5156 #endif | |
5157 { | |
5158 ff_visited_T *vp; | |
5159 #ifdef UNIX | |
5160 struct stat st; | |
5161 int url = FALSE; | |
5162 #endif | |
5163 | |
5164 /* For an URL we only compare the name, otherwise we compare the | |
5165 * device/inode (unix) or the full path name (not Unix). */ | |
5166 if (path_with_url(fname)) | |
5167 { | |
418 | 5168 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1); |
7 | 5169 #ifdef UNIX |
5170 url = TRUE; | |
5171 #endif | |
5172 } | |
5173 else | |
5174 { | |
5175 ff_expand_buffer[0] = NUL; | |
5176 #ifdef UNIX | |
5177 if (mch_stat((char *)fname, &st) < 0) | |
5178 #else | |
5179 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) | |
5180 #endif | |
5181 return FAIL; | |
5182 } | |
5183 | |
5184 /* check against list of already visited files */ | |
5185 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next) | |
5186 { | |
5187 if ( | |
5188 #ifdef UNIX | |
1881 | 5189 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev |
5190 && vp->ffv_ino == st.st_ino) | |
5191 : | |
7 | 5192 #endif |
5193 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0 | |
5194 ) | |
5195 { | |
5196 #ifdef FEAT_PATH_EXTRA | |
5197 /* are the wildcard parts equal */ | |
5198 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE) | |
5199 #endif | |
5200 /* already visited */ | |
5201 return FAIL; | |
5202 } | |
5203 } | |
5204 | |
5205 /* | |
5206 * New file/dir. Add it to the list of visited files/dirs. | |
5207 */ | |
5208 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T) | |
5209 + STRLEN(ff_expand_buffer))); | |
5210 | |
5211 if (vp != NULL) | |
5212 { | |
5213 #ifdef UNIX | |
5214 if (!url) | |
5215 { | |
1881 | 5216 vp->ffv_dev_valid = TRUE; |
7 | 5217 vp->ffv_ino = st.st_ino; |
5218 vp->ffv_dev = st.st_dev; | |
5219 vp->ffv_fname[0] = NUL; | |
5220 } | |
5221 else | |
5222 { | |
1881 | 5223 vp->ffv_dev_valid = FALSE; |
7 | 5224 #endif |
5225 STRCPY(vp->ffv_fname, ff_expand_buffer); | |
5226 #ifdef UNIX | |
5227 } | |
5228 #endif | |
5229 #ifdef FEAT_PATH_EXTRA | |
5230 if (wc_path != NULL) | |
5231 vp->ffv_wc_path = vim_strsave(wc_path); | |
5232 else | |
5233 vp->ffv_wc_path = NULL; | |
5234 #endif | |
5235 | |
5236 vp->ffv_next = *visited_list; | |
5237 *visited_list = vp; | |
5238 } | |
5239 | |
5240 return OK; | |
5241 } | |
5242 | |
5243 /* | |
5244 * create stack element from given path pieces | |
5245 */ | |
5246 static ff_stack_T * | |
5247 ff_create_stack_element(fix_part, | |
5248 #ifdef FEAT_PATH_EXTRA | |
5249 wc_part, | |
5250 #endif | |
5251 level, star_star_empty) | |
5252 char_u *fix_part; | |
5253 #ifdef FEAT_PATH_EXTRA | |
5254 char_u *wc_part; | |
5255 #endif | |
5256 int level; | |
5257 int star_star_empty; | |
5258 { | |
5259 ff_stack_T *new; | |
5260 | |
5261 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T)); | |
5262 if (new == NULL) | |
5263 return NULL; | |
5264 | |
5265 new->ffs_prev = NULL; | |
5266 new->ffs_filearray = NULL; | |
5267 new->ffs_filearray_size = 0; | |
5268 new->ffs_filearray_cur = 0; | |
5269 new->ffs_stage = 0; | |
5270 new->ffs_level = level; | |
5271 new->ffs_star_star_empty = star_star_empty;; | |
5272 | |
5273 /* the following saves NULL pointer checks in vim_findfile */ | |
5274 if (fix_part == NULL) | |
5275 fix_part = (char_u *)""; | |
5276 new->ffs_fix_path = vim_strsave(fix_part); | |
5277 | |
5278 #ifdef FEAT_PATH_EXTRA | |
5279 if (wc_part == NULL) | |
5280 wc_part = (char_u *)""; | |
5281 new->ffs_wc_path = vim_strsave(wc_part); | |
5282 #endif | |
5283 | |
5284 if (new->ffs_fix_path == NULL | |
5285 #ifdef FEAT_PATH_EXTRA | |
5286 || new->ffs_wc_path == NULL | |
5287 #endif | |
5288 ) | |
5289 { | |
5290 ff_free_stack_element(new); | |
5291 new = NULL; | |
5292 } | |
5293 | |
5294 return new; | |
5295 } | |
5296 | |
5297 /* | |
1541 | 5298 * Push a dir on the directory stack. |
7 | 5299 */ |
5300 static void | |
1541 | 5301 ff_push(search_ctx, stack_ptr) |
5302 ff_search_ctx_T *search_ctx; | |
5303 ff_stack_T *stack_ptr; | |
7 | 5304 { |
5305 /* check for NULL pointer, not to return an error to the user, but | |
359 | 5306 * to prevent a crash */ |
1541 | 5307 if (stack_ptr != NULL) |
7 | 5308 { |
1541 | 5309 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr; |
5310 search_ctx->ffsc_stack_ptr = stack_ptr; | |
7 | 5311 } |
5312 } | |
5313 | |
5314 /* | |
1541 | 5315 * Pop a dir from the directory stack. |
5316 * Returns NULL if stack is empty. | |
7 | 5317 */ |
5318 static ff_stack_T * | |
1541 | 5319 ff_pop(search_ctx) |
5320 ff_search_ctx_T *search_ctx; | |
7 | 5321 { |
5322 ff_stack_T *sptr; | |
5323 | |
1541 | 5324 sptr = search_ctx->ffsc_stack_ptr; |
5325 if (search_ctx->ffsc_stack_ptr != NULL) | |
5326 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev; | |
7 | 5327 |
5328 return sptr; | |
5329 } | |
5330 | |
5331 /* | |
5332 * free the given stack element | |
5333 */ | |
5334 static void | |
1541 | 5335 ff_free_stack_element(stack_ptr) |
5336 ff_stack_T *stack_ptr; | |
7 | 5337 { |
5338 /* vim_free handles possible NULL pointers */ | |
1541 | 5339 vim_free(stack_ptr->ffs_fix_path); |
7 | 5340 #ifdef FEAT_PATH_EXTRA |
1541 | 5341 vim_free(stack_ptr->ffs_wc_path); |
5342 #endif | |
5343 | |
5344 if (stack_ptr->ffs_filearray != NULL) | |
5345 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray); | |
5346 | |
5347 vim_free(stack_ptr); | |
7 | 5348 } |
5349 | |
5350 /* | |
1541 | 5351 * Clear the search context, but NOT the visited list. |
7 | 5352 */ |
5353 static void | |
1541 | 5354 ff_clear(search_ctx) |
5355 ff_search_ctx_T *search_ctx; | |
7 | 5356 { |
5357 ff_stack_T *sptr; | |
5358 | |
5359 /* clear up stack */ | |
1541 | 5360 while ((sptr = ff_pop(search_ctx)) != NULL) |
7 | 5361 ff_free_stack_element(sptr); |
5362 | |
1541 | 5363 vim_free(search_ctx->ffsc_file_to_search); |
5364 vim_free(search_ctx->ffsc_start_dir); | |
5365 vim_free(search_ctx->ffsc_fix_path); | |
7 | 5366 #ifdef FEAT_PATH_EXTRA |
1541 | 5367 vim_free(search_ctx->ffsc_wc_path); |
7 | 5368 #endif |
5369 | |
5370 #ifdef FEAT_PATH_EXTRA | |
1541 | 5371 if (search_ctx->ffsc_stopdirs_v != NULL) |
7 | 5372 { |
5373 int i = 0; | |
5374 | |
1541 | 5375 while (search_ctx->ffsc_stopdirs_v[i] != NULL) |
7 | 5376 { |
1541 | 5377 vim_free(search_ctx->ffsc_stopdirs_v[i]); |
7 | 5378 i++; |
5379 } | |
1541 | 5380 vim_free(search_ctx->ffsc_stopdirs_v); |
7 | 5381 } |
1541 | 5382 search_ctx->ffsc_stopdirs_v = NULL; |
7 | 5383 #endif |
5384 | |
5385 /* reset everything */ | |
1541 | 5386 search_ctx->ffsc_file_to_search = NULL; |
5387 search_ctx->ffsc_start_dir = NULL; | |
5388 search_ctx->ffsc_fix_path = NULL; | |
7 | 5389 #ifdef FEAT_PATH_EXTRA |
1541 | 5390 search_ctx->ffsc_wc_path = NULL; |
5391 search_ctx->ffsc_level = 0; | |
7 | 5392 #endif |
5393 } | |
5394 | |
5395 #ifdef FEAT_PATH_EXTRA | |
5396 /* | |
5397 * check if the given path is in the stopdirs | |
5398 * returns TRUE if yes else FALSE | |
5399 */ | |
5400 static int | |
5401 ff_path_in_stoplist(path, path_len, stopdirs_v) | |
5402 char_u *path; | |
5403 int path_len; | |
5404 char_u **stopdirs_v; | |
5405 { | |
5406 int i = 0; | |
5407 | |
5408 /* eat up trailing path separators, except the first */ | |
461 | 5409 while (path_len > 1 && vim_ispathsep(path[path_len - 1])) |
7 | 5410 path_len--; |
5411 | |
5412 /* if no path consider it as match */ | |
5413 if (path_len == 0) | |
5414 return TRUE; | |
5415 | |
5416 for (i = 0; stopdirs_v[i] != NULL; i++) | |
5417 { | |
5418 if ((int)STRLEN(stopdirs_v[i]) > path_len) | |
5419 { | |
5420 /* match for parent directory. So '/home' also matches | |
5421 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else | |
5422 * '/home/r' would also match '/home/rks' | |
5423 */ | |
5424 if (fnamencmp(stopdirs_v[i], path, path_len) == 0 | |
461 | 5425 && vim_ispathsep(stopdirs_v[i][path_len])) |
7 | 5426 return TRUE; |
5427 } | |
5428 else | |
5429 { | |
5430 if (fnamecmp(stopdirs_v[i], path) == 0) | |
5431 return TRUE; | |
5432 } | |
5433 } | |
5434 return FALSE; | |
5435 } | |
5436 #endif | |
5437 | |
5438 #if defined(FEAT_SEARCHPATH) || defined(PROTO) | |
5439 /* | |
1541 | 5440 * Find the file name "ptr[len]" in the path. Also finds directory names. |
7 | 5441 * |
5442 * On the first call set the parameter 'first' to TRUE to initialize | |
5443 * the search. For repeating calls to FALSE. | |
5444 * | |
5445 * Repeating calls will return other files called 'ptr[len]' from the path. | |
5446 * | |
5447 * Only on the first call 'ptr' and 'len' are used. For repeating calls they | |
5448 * don't need valid values. | |
5449 * | |
5450 * If nothing found on the first call the option FNAME_MESS will issue the | |
5451 * message: | |
5452 * 'Can't find file "<file>" in path' | |
5453 * On repeating calls: | |
5454 * 'No more file "<file>" found in path' | |
5455 * | |
5456 * options: | |
5457 * FNAME_MESS give error message when not found | |
5458 * | |
5459 * Uses NameBuff[]! | |
5460 * | |
5461 * Returns an allocated string for the file name. NULL for error. | |
5462 * | |
5463 */ | |
5464 char_u * | |
5465 find_file_in_path(ptr, len, options, first, rel_fname) | |
5466 char_u *ptr; /* file name */ | |
5467 int len; /* length of file name */ | |
5468 int options; | |
5469 int first; /* use count'th matching file name */ | |
5470 char_u *rel_fname; /* file name searching relative to */ | |
5471 { | |
5472 return find_file_in_path_option(ptr, len, options, first, | |
5473 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path, | |
1541 | 5474 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua); |
7 | 5475 } |
5476 | |
359 | 5477 static char_u *ff_file_to_find = NULL; |
5478 static void *fdip_search_ctx = NULL; | |
5479 | |
5480 #if defined(EXITFREE) | |
5481 static void | |
5482 free_findfile() | |
5483 { | |
5484 vim_free(ff_file_to_find); | |
5485 vim_findfile_cleanup(fdip_search_ctx); | |
5486 } | |
5487 #endif | |
5488 | |
7 | 5489 /* |
5490 * Find the directory name "ptr[len]" in the path. | |
5491 * | |
5492 * options: | |
5493 * FNAME_MESS give error message when not found | |
6633 | 5494 * FNAME_UNESC unescape backslashes. |
7 | 5495 * |
5496 * Uses NameBuff[]! | |
5497 * | |
5498 * Returns an allocated string for the file name. NULL for error. | |
5499 */ | |
5500 char_u * | |
5501 find_directory_in_path(ptr, len, options, rel_fname) | |
5502 char_u *ptr; /* file name */ | |
5503 int len; /* length of file name */ | |
5504 int options; | |
5505 char_u *rel_fname; /* file name searching relative to */ | |
5506 { | |
5507 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath, | |
1541 | 5508 FINDFILE_DIR, rel_fname, (char_u *)""); |
7 | 5509 } |
5510 | |
19 | 5511 char_u * |
6633 | 5512 find_file_in_path_option(ptr, len, options, first, path_option, |
5513 find_what, rel_fname, suffixes) | |
7 | 5514 char_u *ptr; /* file name */ |
5515 int len; /* length of file name */ | |
5516 int options; | |
5517 int first; /* use count'th matching file name */ | |
5518 char_u *path_option; /* p_path or p_cdpath */ | |
1541 | 5519 int find_what; /* FINDFILE_FILE, _DIR or _BOTH */ |
7 | 5520 char_u *rel_fname; /* file name we are looking relative to. */ |
794 | 5521 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */ |
7 | 5522 { |
5523 static char_u *dir; | |
5524 static int did_findfile_init = FALSE; | |
5525 char_u save_char; | |
5526 char_u *file_name = NULL; | |
5527 char_u *buf = NULL; | |
5528 int rel_to_curdir; | |
5529 #ifdef AMIGA | |
5530 struct Process *proc = (struct Process *)FindTask(0L); | |
5531 APTR save_winptr = proc->pr_WindowPtr; | |
5532 | |
5533 /* Avoid a requester here for a volume that doesn't exist. */ | |
5534 proc->pr_WindowPtr = (APTR)-1L; | |
5535 #endif | |
5536 | |
5537 if (first == TRUE) | |
5538 { | |
5539 /* copy file name into NameBuff, expanding environment variables */ | |
5540 save_char = ptr[len]; | |
5541 ptr[len] = NUL; | |
5542 expand_env(ptr, NameBuff, MAXPATHL); | |
5543 ptr[len] = save_char; | |
5544 | |
359 | 5545 vim_free(ff_file_to_find); |
5546 ff_file_to_find = vim_strsave(NameBuff); | |
5547 if (ff_file_to_find == NULL) /* out of memory */ | |
7 | 5548 { |
5549 file_name = NULL; | |
5550 goto theend; | |
5551 } | |
6633 | 5552 if (options & FNAME_UNESC) |
5553 { | |
5554 /* Change all "\ " to " ". */ | |
5555 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr) | |
5556 if (ptr[0] == '\\' && ptr[1] == ' ') | |
5557 mch_memmove(ptr, ptr + 1, STRLEN(ptr)); | |
5558 } | |
7 | 5559 } |
5560 | |
359 | 5561 rel_to_curdir = (ff_file_to_find[0] == '.' |
5562 && (ff_file_to_find[1] == NUL | |
5563 || vim_ispathsep(ff_file_to_find[1]) | |
5564 || (ff_file_to_find[1] == '.' | |
5565 && (ff_file_to_find[2] == NUL | |
5566 || vim_ispathsep(ff_file_to_find[2]))))); | |
5567 if (vim_isAbsName(ff_file_to_find) | |
7 | 5568 /* "..", "../path", "." and "./path": don't use the path_option */ |
5569 || rel_to_curdir | |
7408
1886f2863437
commit https://github.com/vim/vim/commit/e7fedb6ebe72d9a475aa65109b77d5ed4667067a
Christian Brabandt <cb@256bit.org>
parents:
7277
diff
changeset
|
5570 #if defined(MSWIN) || defined(MSDOS) |
7 | 5571 /* handle "\tmp" as absolute path */ |
359 | 5572 || vim_ispathsep(ff_file_to_find[0]) |
1993 | 5573 /* handle "c:name" as absolute path */ |
359 | 5574 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':') |
7 | 5575 #endif |
5576 #ifdef AMIGA | |
5577 /* handle ":tmp" as absolute path */ | |
359 | 5578 || ff_file_to_find[0] == ':' |
7 | 5579 #endif |
5580 ) | |
5581 { | |
5582 /* | |
5583 * Absolute path, no need to use "path_option". | |
5584 * If this is not a first call, return NULL. We already returned a | |
5585 * filename on the first call. | |
5586 */ | |
5587 if (first == TRUE) | |
5588 { | |
5589 int l; | |
5590 int run; | |
5591 | |
359 | 5592 if (path_with_url(ff_file_to_find)) |
7 | 5593 { |
359 | 5594 file_name = vim_strsave(ff_file_to_find); |
7 | 5595 goto theend; |
5596 } | |
5597 | |
5598 /* When FNAME_REL flag given first use the directory of the file. | |
5599 * Otherwise or when this fails use the current directory. */ | |
5600 for (run = 1; run <= 2; ++run) | |
5601 { | |
359 | 5602 l = (int)STRLEN(ff_file_to_find); |
7 | 5603 if (run == 1 |
5604 && rel_to_curdir | |
5605 && (options & FNAME_REL) | |
5606 && rel_fname != NULL | |
5607 && STRLEN(rel_fname) + l < MAXPATHL) | |
5608 { | |
5609 STRCPY(NameBuff, rel_fname); | |
359 | 5610 STRCPY(gettail(NameBuff), ff_file_to_find); |
7 | 5611 l = (int)STRLEN(NameBuff); |
5612 } | |
5613 else | |
5614 { | |
359 | 5615 STRCPY(NameBuff, ff_file_to_find); |
7 | 5616 run = 2; |
5617 } | |
5618 | |
5619 /* When the file doesn't exist, try adding parts of | |
5620 * 'suffixesadd'. */ | |
794 | 5621 buf = suffixes; |
7 | 5622 for (;;) |
5623 { | |
5624 if ( | |
5625 #ifdef DJGPP | |
5626 /* "C:" by itself will fail for mch_getperm(), | |
5627 * assume it's always valid. */ | |
1541 | 5628 (find_what != FINDFILE_FILE && NameBuff[0] != NUL |
7 | 5629 && NameBuff[1] == ':' |
5630 && NameBuff[2] == NUL) || | |
5631 #endif | |
5632 (mch_getperm(NameBuff) >= 0 | |
1541 | 5633 && (find_what == FINDFILE_BOTH |
5634 || ((find_what == FINDFILE_DIR) | |
5635 == mch_isdir(NameBuff))))) | |
7 | 5636 { |
5637 file_name = vim_strsave(NameBuff); | |
5638 goto theend; | |
5639 } | |
5640 if (*buf == NUL) | |
5641 break; | |
5642 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ","); | |
5643 } | |
5644 } | |
5645 } | |
5646 } | |
5647 else | |
5648 { | |
5649 /* | |
5650 * Loop over all paths in the 'path' or 'cdpath' option. | |
5651 * When "first" is set, first setup to the start of the option. | |
5652 * Otherwise continue to find the next match. | |
5653 */ | |
5654 if (first == TRUE) | |
5655 { | |
5656 /* vim_findfile_free_visited can handle a possible NULL pointer */ | |
359 | 5657 vim_findfile_free_visited(fdip_search_ctx); |
7 | 5658 dir = path_option; |
5659 did_findfile_init = FALSE; | |
5660 } | |
5661 | |
5662 for (;;) | |
5663 { | |
5664 if (did_findfile_init) | |
5665 { | |
359 | 5666 file_name = vim_findfile(fdip_search_ctx); |
7 | 5667 if (file_name != NULL) |
5668 break; | |
5669 | |
5670 did_findfile_init = FALSE; | |
5671 } | |
5672 else | |
5673 { | |
5674 char_u *r_ptr; | |
5675 | |
5676 if (dir == NULL || *dir == NUL) | |
5677 { | |
5678 /* We searched all paths of the option, now we can | |
5679 * free the search context. */ | |
359 | 5680 vim_findfile_cleanup(fdip_search_ctx); |
5681 fdip_search_ctx = NULL; | |
7 | 5682 break; |
5683 } | |
5684 | |
5685 if ((buf = alloc((int)(MAXPATHL))) == NULL) | |
5686 break; | |
5687 | |
5688 /* copy next path */ | |
5689 buf[0] = 0; | |
5690 copy_option_part(&dir, buf, MAXPATHL, " ,"); | |
5691 | |
5692 #ifdef FEAT_PATH_EXTRA | |
5693 /* get the stopdir string */ | |
5694 r_ptr = vim_findfile_stopdir(buf); | |
5695 #else | |
5696 r_ptr = NULL; | |
5697 #endif | |
359 | 5698 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find, |
1541 | 5699 r_ptr, 100, FALSE, find_what, |
359 | 5700 fdip_search_ctx, FALSE, rel_fname); |
5701 if (fdip_search_ctx != NULL) | |
7 | 5702 did_findfile_init = TRUE; |
5703 vim_free(buf); | |
5704 } | |
5705 } | |
5706 } | |
5707 if (file_name == NULL && (options & FNAME_MESS)) | |
5708 { | |
5709 if (first == TRUE) | |
5710 { | |
1541 | 5711 if (find_what == FINDFILE_DIR) |
7 | 5712 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"), |
359 | 5713 ff_file_to_find); |
7 | 5714 else |
5715 EMSG2(_("E345: Can't find file \"%s\" in path"), | |
359 | 5716 ff_file_to_find); |
7 | 5717 } |
5718 else | |
5719 { | |
1541 | 5720 if (find_what == FINDFILE_DIR) |
7 | 5721 EMSG2(_("E346: No more directory \"%s\" found in cdpath"), |
359 | 5722 ff_file_to_find); |
7 | 5723 else |
5724 EMSG2(_("E347: No more file \"%s\" found in path"), | |
359 | 5725 ff_file_to_find); |
7 | 5726 } |
5727 } | |
5728 | |
5729 theend: | |
5730 #ifdef AMIGA | |
5731 proc->pr_WindowPtr = save_winptr; | |
5732 #endif | |
5733 return file_name; | |
5734 } | |
5735 | |
5736 #endif /* FEAT_SEARCHPATH */ | |
5737 | |
5738 /* | |
5739 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search | |
5740 * 'cdpath' for relative directory names, otherwise just mch_chdir(). | |
5741 */ | |
5742 int | |
5743 vim_chdir(new_dir) | |
5744 char_u *new_dir; | |
5745 { | |
5746 #ifndef FEAT_SEARCHPATH | |
5747 return mch_chdir((char *)new_dir); | |
5748 #else | |
5749 char_u *dir_name; | |
5750 int r; | |
5751 | |
5752 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir), | |
5753 FNAME_MESS, curbuf->b_ffname); | |
5754 if (dir_name == NULL) | |
5755 return -1; | |
5756 r = mch_chdir((char *)dir_name); | |
5757 vim_free(dir_name); | |
5758 return r; | |
5759 #endif | |
5760 } | |
5761 | |
5762 /* | |
418 | 5763 * Get user name from machine-specific function. |
7 | 5764 * Returns the user name in "buf[len]". |
418 | 5765 * Some systems are quite slow in obtaining the user name (Windows NT), thus |
5766 * cache the result. | |
7 | 5767 * Returns OK or FAIL. |
5768 */ | |
5769 int | |
5770 get_user_name(buf, len) | |
5771 char_u *buf; | |
5772 int len; | |
5773 { | |
359 | 5774 if (username == NULL) |
7 | 5775 { |
5776 if (mch_get_user_name(buf, len) == FAIL) | |
5777 return FAIL; | |
359 | 5778 username = vim_strsave(buf); |
7 | 5779 } |
5780 else | |
418 | 5781 vim_strncpy(buf, username, len - 1); |
7 | 5782 return OK; |
5783 } | |
5784 | |
5785 #ifndef HAVE_QSORT | |
5786 /* | |
5787 * Our own qsort(), for systems that don't have it. | |
5788 * It's simple and slow. From the K&R C book. | |
5789 */ | |
5790 void | |
5791 qsort(base, elm_count, elm_size, cmp) | |
5792 void *base; | |
5793 size_t elm_count; | |
5794 size_t elm_size; | |
5795 int (*cmp) __ARGS((const void *, const void *)); | |
5796 { | |
5797 char_u *buf; | |
5798 char_u *p1; | |
5799 char_u *p2; | |
5800 int i, j; | |
5801 int gap; | |
5802 | |
5803 buf = alloc((unsigned)elm_size); | |
5804 if (buf == NULL) | |
5805 return; | |
5806 | |
5807 for (gap = elm_count / 2; gap > 0; gap /= 2) | |
5808 for (i = gap; i < elm_count; ++i) | |
5809 for (j = i - gap; j >= 0; j -= gap) | |
5810 { | |
5811 /* Compare the elements. */ | |
5812 p1 = (char_u *)base + j * elm_size; | |
5813 p2 = (char_u *)base + (j + gap) * elm_size; | |
5814 if ((*cmp)((void *)p1, (void *)p2) <= 0) | |
5815 break; | |
1993 | 5816 /* Exchange the elements. */ |
7 | 5817 mch_memmove(buf, p1, elm_size); |
5818 mch_memmove(p1, p2, elm_size); | |
5819 mch_memmove(p2, buf, elm_size); | |
5820 } | |
5821 | |
5822 vim_free(buf); | |
5823 } | |
5824 #endif | |
5825 | |
5826 /* | |
5827 * Sort an array of strings. | |
5828 */ | |
5829 static int | |
5830 #ifdef __BORLANDC__ | |
5831 _RTLENTRYF | |
5832 #endif | |
5833 sort_compare __ARGS((const void *s1, const void *s2)); | |
5834 | |
5835 static int | |
5836 #ifdef __BORLANDC__ | |
5837 _RTLENTRYF | |
5838 #endif | |
5839 sort_compare(s1, s2) | |
5840 const void *s1; | |
5841 const void *s2; | |
5842 { | |
5843 return STRCMP(*(char **)s1, *(char **)s2); | |
5844 } | |
5845 | |
5846 void | |
5847 sort_strings(files, count) | |
5848 char_u **files; | |
5849 int count; | |
5850 { | |
5851 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare); | |
5852 } | |
5853 | |
5854 #if !defined(NO_EXPANDPATH) || defined(PROTO) | |
5855 /* | |
5856 * Compare path "p[]" to "q[]". | |
39 | 5857 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]" |
7 | 5858 * Return value like strcmp(p, q), but consider path separators. |
5859 */ | |
5860 int | |
39 | 5861 pathcmp(p, q, maxlen) |
7 | 5862 const char *p, *q; |
39 | 5863 int maxlen; |
7 | 5864 { |
7044
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5865 int i, j; |
4276 | 5866 int c1, c2; |
41 | 5867 const char *s = NULL; |
7 | 5868 |
7044
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5869 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);) |
7 | 5870 { |
4276 | 5871 c1 = PTR2CHAR((char_u *)p + i); |
7044
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5872 c2 = PTR2CHAR((char_u *)q + j); |
4276 | 5873 |
7 | 5874 /* End of "p": check if "q" also ends or just has a slash. */ |
4276 | 5875 if (c1 == NUL) |
7 | 5876 { |
4276 | 5877 if (c2 == NUL) /* full match */ |
7 | 5878 return 0; |
5879 s = q; | |
7044
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5880 i = j; |
7 | 5881 break; |
5882 } | |
5883 | |
5884 /* End of "q": check if "p" just has a slash. */ | |
4276 | 5885 if (c2 == NUL) |
7 | 5886 { |
5887 s = p; | |
5888 break; | |
5889 } | |
5890 | |
4276 | 5891 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2) |
7 | 5892 #ifdef BACKSLASH_IN_FILENAME |
5893 /* consider '/' and '\\' to be equal */ | |
4276 | 5894 && !((c1 == '/' && c2 == '\\') |
5895 || (c1 == '\\' && c2 == '/')) | |
7 | 5896 #endif |
5897 ) | |
5898 { | |
4276 | 5899 if (vim_ispathsep(c1)) |
7 | 5900 return -1; |
4276 | 5901 if (vim_ispathsep(c2)) |
7 | 5902 return 1; |
4276 | 5903 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2) |
5904 : c1 - c2; /* no match */ | |
7 | 5905 } |
7044
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5906 |
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5907 i += MB_PTR2LEN((char_u *)p + i); |
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5908 j += MB_PTR2LEN((char_u *)q + j); |
7 | 5909 } |
7044
7758d6245c3a
commit https://github.com/vim/vim/commit/f6470c288cb6f8efd60a507baf2c070f9d209ae6
Christian Brabandt <cb@256bit.org>
parents:
6929
diff
changeset
|
5910 if (s == NULL) /* "i" or "j" ran into "maxlen" */ |
41 | 5911 return 0; |
7 | 5912 |
4276 | 5913 c1 = PTR2CHAR((char_u *)s + i); |
5914 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i)); | |
7 | 5915 /* ignore a trailing slash, but not "//" or ":/" */ |
4276 | 5916 if (c2 == NUL |
41 | 5917 && i > 0 |
5918 && !after_pathsep((char_u *)s, (char_u *)s + i) | |
7 | 5919 #ifdef BACKSLASH_IN_FILENAME |
4276 | 5920 && (c1 == '/' || c1 == '\\') |
7 | 5921 #else |
4276 | 5922 && c1 == '/' |
7 | 5923 #endif |
41 | 5924 ) |
7 | 5925 return 0; /* match with trailing slash */ |
5926 if (s == q) | |
5927 return -1; /* no match */ | |
5928 return 1; | |
5929 } | |
5930 #endif | |
5931 | |
5932 /* | |
5933 * The putenv() implementation below comes from the "screen" program. | |
5934 * Included with permission from Juergen Weigert. | |
5935 * See pty.c for the copyright notice. | |
5936 */ | |
5937 | |
5938 /* | |
5939 * putenv -- put value into environment | |
5940 * | |
5941 * Usage: i = putenv (string) | |
5942 * int i; | |
5943 * char *string; | |
5944 * | |
5945 * where string is of the form <name>=<value>. | |
5946 * Putenv returns 0 normally, -1 on error (not enough core for malloc). | |
5947 * | |
5948 * Putenv may need to add a new name into the environment, or to | |
5949 * associate a value longer than the current value with a particular | |
5950 * name. So, to make life simpler, putenv() copies your entire | |
5951 * environment into the heap (i.e. malloc()) from the stack | |
5952 * (i.e. where it resides when your process is initiated) the first | |
5953 * time you call it. | |
5954 * | |
5955 * (history removed, not very interesting. See the "screen" sources.) | |
5956 */ | |
5957 | |
5958 #if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) | |
5959 | |
5960 #define EXTRASIZE 5 /* increment to add to env. size */ | |
5961 | |
5962 static int envsize = -1; /* current size of environment */ | |
5963 #ifndef MACOS_CLASSIC | |
5964 extern | |
5965 #endif | |
5966 char **environ; /* the global which is your env. */ | |
5967 | |
5968 static int findenv __ARGS((char *name)); /* look for a name in the env. */ | |
5969 static int newenv __ARGS((void)); /* copy env. from stack to heap */ | |
5970 static int moreenv __ARGS((void)); /* incr. size of env. */ | |
5971 | |
5972 int | |
5973 putenv(string) | |
5974 const char *string; | |
5975 { | |
5976 int i; | |
5977 char *p; | |
5978 | |
5979 if (envsize < 0) | |
5980 { /* first time putenv called */ | |
5981 if (newenv() < 0) /* copy env. to heap */ | |
5982 return -1; | |
5983 } | |
5984 | |
5985 i = findenv((char *)string); /* look for name in environment */ | |
5986 | |
5987 if (i < 0) | |
5988 { /* name must be added */ | |
5989 for (i = 0; environ[i]; i++); | |
5990 if (i >= (envsize - 1)) | |
5991 { /* need new slot */ | |
5992 if (moreenv() < 0) | |
5993 return -1; | |
5994 } | |
5995 p = (char *)alloc((unsigned)(strlen(string) + 1)); | |
5996 if (p == NULL) /* not enough core */ | |
5997 return -1; | |
5998 environ[i + 1] = 0; /* new end of env. */ | |
5999 } | |
6000 else | |
6001 { /* name already in env. */ | |
6002 p = vim_realloc(environ[i], strlen(string) + 1); | |
6003 if (p == NULL) | |
6004 return -1; | |
6005 } | |
6006 sprintf(p, "%s", string); /* copy into env. */ | |
6007 environ[i] = p; | |
6008 | |
6009 return 0; | |
6010 } | |
6011 | |
6012 static int | |
6013 findenv(name) | |
6014 char *name; | |
6015 { | |
6016 char *namechar, *envchar; | |
6017 int i, found; | |
6018 | |
6019 found = 0; | |
6020 for (i = 0; environ[i] && !found; i++) | |
6021 { | |
6022 envchar = environ[i]; | |
6023 namechar = name; | |
6024 while (*namechar && *namechar != '=' && (*namechar == *envchar)) | |
6025 { | |
6026 namechar++; | |
6027 envchar++; | |
6028 } | |
6029 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '='); | |
6030 } | |
6031 return found ? i - 1 : -1; | |
6032 } | |
6033 | |
6034 static int | |
6035 newenv() | |
6036 { | |
6037 char **env, *elem; | |
6038 int i, esize; | |
6039 | |
6040 #ifdef MACOS | |
6041 /* for Mac a new, empty environment is created */ | |
6042 i = 0; | |
6043 #else | |
6044 for (i = 0; environ[i]; i++) | |
6045 ; | |
6046 #endif | |
6047 esize = i + EXTRASIZE + 1; | |
6048 env = (char **)alloc((unsigned)(esize * sizeof (elem))); | |
6049 if (env == NULL) | |
6050 return -1; | |
6051 | |
6052 #ifndef MACOS | |
6053 for (i = 0; environ[i]; i++) | |
6054 { | |
6055 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1)); | |
6056 if (elem == NULL) | |
6057 return -1; | |
6058 env[i] = elem; | |
6059 strcpy(elem, environ[i]); | |
6060 } | |
6061 #endif | |
6062 | |
6063 env[i] = 0; | |
6064 environ = env; | |
6065 envsize = esize; | |
6066 return 0; | |
6067 } | |
6068 | |
6069 static int | |
6070 moreenv() | |
6071 { | |
6072 int esize; | |
6073 char **env; | |
6074 | |
6075 esize = envsize + EXTRASIZE; | |
6076 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env)); | |
6077 if (env == 0) | |
6078 return -1; | |
6079 environ = env; | |
6080 envsize = esize; | |
6081 return 0; | |
6082 } | |
6083 | |
6084 # ifdef USE_VIMPTY_GETENV | |
6085 char_u * | |
6086 vimpty_getenv(string) | |
6087 const char_u *string; | |
6088 { | |
6089 int i; | |
6090 char_u *p; | |
6091 | |
6092 if (envsize < 0) | |
6093 return NULL; | |
6094 | |
6095 i = findenv((char *)string); | |
6096 | |
6097 if (i < 0) | |
6098 return NULL; | |
6099 | |
6100 p = vim_strchr((char_u *)environ[i], '='); | |
6101 return (p + 1); | |
6102 } | |
6103 # endif | |
6104 | |
6105 #endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */ | |
313 | 6106 |
741 | 6107 #if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO) |
313 | 6108 /* |
6109 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have | |
6110 * rights to write into. | |
6111 */ | |
6112 int | |
6113 filewritable(fname) | |
6114 char_u *fname; | |
6115 { | |
6116 int retval = 0; | |
6117 #if defined(UNIX) || defined(VMS) | |
6118 int perm = 0; | |
6119 #endif | |
6120 | |
6121 #if defined(UNIX) || defined(VMS) | |
6122 perm = mch_getperm(fname); | |
6123 #endif | |
6124 #ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */ | |
6125 if ( | |
6126 # ifdef WIN3264 | |
6127 mch_writable(fname) && | |
6128 # else | |
6129 # if defined(UNIX) || defined(VMS) | |
6130 (perm & 0222) && | |
6131 # endif | |
6132 # endif | |
6133 mch_access((char *)fname, W_OK) == 0 | |
6134 ) | |
6135 #endif | |
6136 { | |
6137 ++retval; | |
6138 if (mch_isdir(fname)) | |
6139 ++retval; | |
6140 } | |
6141 return retval; | |
6142 } | |
6143 #endif | |
332 | 6144 |
6145 /* | |
6146 * Print an error message with one or two "%s" and one or two string arguments. | |
6147 * This is not in message.c to avoid a warning for prototypes. | |
6148 */ | |
6149 int | |
6150 emsg3(s, a1, a2) | |
6151 char_u *s, *a1, *a2; | |
6152 { | |
840 | 6153 if (emsg_not_now()) |
332 | 6154 return TRUE; /* no error messages at the moment */ |
1438 | 6155 #ifdef HAVE_STDARG_H |
6156 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2); | |
6157 #else | |
840 | 6158 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2); |
1438 | 6159 #endif |
332 | 6160 return emsg(IObuff); |
6161 } | |
6162 | |
6163 /* | |
6164 * Print an error message with one "%ld" and one long int argument. | |
6165 * This is not in message.c to avoid a warning for prototypes. | |
6166 */ | |
6167 int | |
6168 emsgn(s, n) | |
6169 char_u *s; | |
6170 long n; | |
6171 { | |
840 | 6172 if (emsg_not_now()) |
332 | 6173 return TRUE; /* no error messages at the moment */ |
6174 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n); | |
6175 return emsg(IObuff); | |
6176 } | |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6177 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6178 #if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6179 /* |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6180 * Read 2 bytes from "fd" and turn them into an int, MSB first. |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6181 */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6182 int |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6183 get2c(fd) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6184 FILE *fd; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6185 { |
2231
aa6412cab544
Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6186 int n; |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6187 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6188 n = getc(fd); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6189 n = (n << 8) + getc(fd); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6190 return n; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6191 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6192 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6193 /* |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6194 * Read 3 bytes from "fd" and turn them into an int, MSB first. |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6195 */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6196 int |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6197 get3c(fd) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6198 FILE *fd; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6199 { |
2231
aa6412cab544
Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6200 int n; |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6201 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6202 n = getc(fd); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6203 n = (n << 8) + getc(fd); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6204 n = (n << 8) + getc(fd); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6205 return n; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6206 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6207 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6208 /* |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6209 * Read 4 bytes from "fd" and turn them into an int, MSB first. |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6210 */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6211 int |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6212 get4c(fd) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6213 FILE *fd; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6214 { |
5347 | 6215 /* Use unsigned rather than int otherwise result is undefined |
6216 * when left-shift sets the MSB. */ | |
6217 unsigned n; | |
6218 | |
6219 n = (unsigned)getc(fd); | |
6220 n = (n << 8) + (unsigned)getc(fd); | |
6221 n = (n << 8) + (unsigned)getc(fd); | |
6222 n = (n << 8) + (unsigned)getc(fd); | |
6223 return (int)n; | |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6224 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6225 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6226 /* |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6227 * Read 8 bytes from "fd" and turn them into a time_t, MSB first. |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6228 */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6229 time_t |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6230 get8ctime(fd) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6231 FILE *fd; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6232 { |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6233 time_t n = 0; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6234 int i; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6235 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6236 for (i = 0; i < 8; ++i) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6237 n = (n << 8) + getc(fd); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6238 return n; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6239 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6240 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6241 /* |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6242 * Read a string of length "cnt" from "fd" into allocated memory. |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6243 * Returns NULL when out of memory or unable to read that many bytes. |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6244 */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6245 char_u * |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6246 read_string(fd, cnt) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6247 FILE *fd; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6248 int cnt; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6249 { |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6250 char_u *str; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6251 int i; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6252 int c; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6253 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6254 /* allocate memory */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6255 str = alloc((unsigned)cnt + 1); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6256 if (str != NULL) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6257 { |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6258 /* Read the string. Quit when running into the EOF. */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6259 for (i = 0; i < cnt; ++i) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6260 { |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6261 c = getc(fd); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6262 if (c == EOF) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6263 { |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6264 vim_free(str); |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6265 return NULL; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6266 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6267 str[i] = c; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6268 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6269 str[i] = NUL; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6270 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6271 return str; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6272 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6273 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6274 /* |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6275 * Write a number to file "fd", MSB first, in "len" bytes. |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6276 */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6277 int |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6278 put_bytes(fd, nr, len) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6279 FILE *fd; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6280 long_u nr; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6281 int len; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6282 { |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6283 int i; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6284 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6285 for (i = len - 1; i >= 0; --i) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6286 if (putc((int)(nr >> (i * 8)), fd) == EOF) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6287 return FAIL; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6288 return OK; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6289 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6290 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6291 #ifdef _MSC_VER |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6292 # if (_MSC_VER <= 1200) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6293 /* This line is required for VC6 without the service pack. Also see the |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6294 * matching #pragma below. */ |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6295 # pragma optimize("", off) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6296 # endif |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6297 #endif |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6298 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6299 /* |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6300 * Write time_t to file "fd" in 8 bytes. |
7519
98fede2c9574
commit https://github.com/vim/vim/commit/285bf84b4b9aca828828a8729b04cd59ab333dac
Christian Brabandt <cb@256bit.org>
parents:
7513
diff
changeset
|
6301 * Returns FAIL when the write failed. |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6302 */ |
7519
98fede2c9574
commit https://github.com/vim/vim/commit/285bf84b4b9aca828828a8729b04cd59ab333dac
Christian Brabandt <cb@256bit.org>
parents:
7513
diff
changeset
|
6303 int |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6304 put_time(fd, the_time) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6305 FILE *fd; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6306 time_t the_time; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6307 { |
6122 | 6308 char_u buf[8]; |
6309 | |
6310 time_to_bytes(the_time, buf); | |
7519
98fede2c9574
commit https://github.com/vim/vim/commit/285bf84b4b9aca828828a8729b04cd59ab333dac
Christian Brabandt <cb@256bit.org>
parents:
7513
diff
changeset
|
6311 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL; |
6122 | 6312 } |
6313 | |
6314 /* | |
6315 * Write time_t to "buf[8]". | |
6316 */ | |
6317 void | |
6318 time_to_bytes(the_time, buf) | |
6319 time_t the_time; | |
6320 char_u *buf; | |
6321 { | |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6322 int c; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6323 int i; |
6122 | 6324 int bi = 0; |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6325 time_t wtime = the_time; |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6326 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6327 /* time_t can be up to 8 bytes in size, more than long_u, thus we |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6328 * can't use put_bytes() here. |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6329 * Another problem is that ">>" may do an arithmetic shift that keeps the |
2232
2e6906bbc5f4
A few more fixes for undo file. Split test in two parts so that it doesn't
Bram Moolenaar <bram@vim.org>
parents:
2231
diff
changeset
|
6330 * sign. This happens for large values of wtime. A cast to long_u may |
2e6906bbc5f4
A few more fixes for undo file. Split test in two parts so that it doesn't
Bram Moolenaar <bram@vim.org>
parents:
2231
diff
changeset
|
6331 * truncate if time_t is 8 bytes. So only use a cast when it is 4 bytes, |
2e6906bbc5f4
A few more fixes for undo file. Split test in two parts so that it doesn't
Bram Moolenaar <bram@vim.org>
parents:
2231
diff
changeset
|
6332 * it's safe to assume that long_u is 4 bytes or more and when using 8 |
2e6906bbc5f4
A few more fixes for undo file. Split test in two parts so that it doesn't
Bram Moolenaar <bram@vim.org>
parents:
2231
diff
changeset
|
6333 * bytes the top bit won't be set. */ |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6334 for (i = 7; i >= 0; --i) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6335 { |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6336 if (i + 1 > (int)sizeof(time_t)) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6337 /* ">>" doesn't work well when shifting more bits than avail */ |
6122 | 6338 buf[bi++] = 0; |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6339 else |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6340 { |
2232
2e6906bbc5f4
A few more fixes for undo file. Split test in two parts so that it doesn't
Bram Moolenaar <bram@vim.org>
parents:
2231
diff
changeset
|
6341 #if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4 |
2245
4e0124f5aee2
Optimize the blowfish crypt/decrypt code a bit more.
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
6342 c = (int)(wtime >> (i * 8)); |
2232
2e6906bbc5f4
A few more fixes for undo file. Split test in two parts so that it doesn't
Bram Moolenaar <bram@vim.org>
parents:
2231
diff
changeset
|
6343 #else |
2245
4e0124f5aee2
Optimize the blowfish crypt/decrypt code a bit more.
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
6344 c = (int)((long_u)wtime >> (i * 8)); |
2232
2e6906bbc5f4
A few more fixes for undo file. Split test in two parts so that it doesn't
Bram Moolenaar <bram@vim.org>
parents:
2231
diff
changeset
|
6345 #endif |
6122 | 6346 buf[bi++] = c; |
2229
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6347 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6348 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6349 } |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6350 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6351 #ifdef _MSC_VER |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6352 # if (_MSC_VER <= 1200) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6353 # pragma optimize("", on) |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6354 # endif |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6355 #endif |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6356 |
d45902a5c61c
Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
6357 #endif |
3257 | 6358 |
6359 #if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \ | |
6360 || defined(FEAT_SPELL) || defined(PROTO) | |
6361 /* | |
6362 * Return TRUE if string "s" contains a non-ASCII character (128 or higher). | |
6363 * When "s" is NULL FALSE is returned. | |
6364 */ | |
6365 int | |
6366 has_non_ascii(s) | |
6367 char_u *s; | |
6368 { | |
6369 char_u *p; | |
6370 | |
6371 if (s != NULL) | |
6372 for (p = s; *p != NUL; ++p) | |
6373 if (*p >= 128) | |
6374 return TRUE; | |
6375 return FALSE; | |
6376 } | |
6377 #endif | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6378 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6379 #if defined(MESSAGE_QUEUE) || defined(PROTO) |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6380 /* |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6381 * Process messages that have been queued for netbeans or clientserver. |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6382 * These functions can call arbitrary vimscript and should only be called when |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6383 * it is safe to do so. |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6384 */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6385 void |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6386 parse_queued_messages() |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6387 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6388 # ifdef FEAT_NETBEANS_INTG |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6389 /* Process the queued netbeans messages. */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6390 netbeans_parse_messages(); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6391 # endif |
7111
57c354f0115c
commit https://github.com/vim/vim/commit/9534680731ea342c2fed01a812559958923480da
Christian Brabandt <cb@256bit.org>
parents:
7109
diff
changeset
|
6392 # if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6393 /* Process the queued clientserver messages. */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6394 server_parse_messages(); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6395 # endif |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6396 } |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7062
diff
changeset
|
6397 #endif |