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