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