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