Mercurial > vim
comparison src/search.c @ 3701:9f48a5432997 v7.3.610
updated for version 7.3.610
Problem: Cannot operate on the text that a search pattern matches.
Solution: Add the "gn" and "gN" commands. (Christian Brabandt)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 25 Jul 2012 15:06:34 +0200 |
parents | d29aa05b7e31 |
children | 0b1cb3f839c4 |
comparison
equal
deleted
inserted
replaced
3700:b21287fb280b | 3701:9f48a5432997 |
---|---|
3395 oap->inclusive = inclusive; | 3395 oap->inclusive = inclusive; |
3396 | 3396 |
3397 return OK; | 3397 return OK; |
3398 } | 3398 } |
3399 | 3399 |
3400 #if defined(FEAT_VISUAL) || defined(PROTO) | |
3401 /* | |
3402 * Find next search match under cursor, cursor at end. | |
3403 * Used while an operator is pending, and in Visual mode. | |
3404 * TODO: redo only works when used in operator pending mode | |
3405 */ | |
3406 int | |
3407 current_search(count, forward) | |
3408 long count; | |
3409 int forward; /* move forward or backwards */ | |
3410 { | |
3411 pos_T start_pos; /* position before the pattern */ | |
3412 pos_T orig_pos; /* position of the cursor at beginning */ | |
3413 pos_T pos; /* position after the pattern */ | |
3414 int i; | |
3415 int dir; | |
3416 int result; /* result of various function calls */ | |
3417 char_u old_p_ws = p_ws; | |
3418 int visual_active = FALSE; | |
3419 int flags = 0; | |
3420 pos_T save_VIsual; | |
3421 | |
3422 | |
3423 /* wrapping should not occur */ | |
3424 p_ws = FALSE; | |
3425 | |
3426 /* Correct cursor when 'selection' is exclusive */ | |
3427 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor)) | |
3428 dec_cursor(); | |
3429 | |
3430 if (VIsual_active) | |
3431 { | |
3432 orig_pos = curwin->w_cursor; | |
3433 save_VIsual = VIsual; | |
3434 visual_active = TRUE; | |
3435 | |
3436 /* just started visual selection, only one character */ | |
3437 if (equalpos(VIsual, curwin->w_cursor)) | |
3438 visual_active = FALSE; | |
3439 | |
3440 pos = curwin->w_cursor; | |
3441 start_pos = VIsual; | |
3442 | |
3443 /* make sure, searching further will extend the match */ | |
3444 if (VIsual_active) | |
3445 { | |
3446 if (forward) | |
3447 incl(&pos); | |
3448 else | |
3449 decl(&pos); | |
3450 } | |
3451 } | |
3452 else | |
3453 orig_pos = pos = start_pos = curwin->w_cursor; | |
3454 | |
3455 /* | |
3456 * The trick is to first search backwards and then search forward again, | |
3457 * so that a match at the current cursor position will be correctly | |
3458 * captured. | |
3459 */ | |
3460 for (i = 0; i < 2; i++) | |
3461 { | |
3462 if (i && count == 1) | |
3463 flags = SEARCH_START; | |
3464 | |
3465 if (forward) | |
3466 dir = i; | |
3467 else | |
3468 dir = !i; | |
3469 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD), | |
3470 spats[last_idx].pat, (long) (i ? count : 1), | |
3471 SEARCH_KEEP | flags | (dir ? 0 : SEARCH_END), | |
3472 RE_SEARCH, 0, NULL); | |
3473 | |
3474 /* First search may fail, but then start searching from the | |
3475 * beginning of the file (cursor might be on the search match) | |
3476 * except when Visual mode is active, so that extending the visual | |
3477 * selection works. */ | |
3478 if (!result && i) /* not found, abort */ | |
3479 { | |
3480 curwin->w_cursor = orig_pos; | |
3481 if (VIsual_active) | |
3482 VIsual = save_VIsual; | |
3483 p_ws = old_p_ws; | |
3484 return FAIL; | |
3485 } | |
3486 else if (!i && !result && !visual_active) | |
3487 { | |
3488 if (forward) /* try again from start of buffer */ | |
3489 { | |
3490 clearpos(&pos); | |
3491 } | |
3492 else /* try again from end of buffer */ | |
3493 { | |
3494 /* searching backwards, so set pos to last line and col */ | |
3495 pos.lnum = curwin->w_buffer->b_ml.ml_line_count; | |
3496 pos.col = STRLEN(ml_get(curwin->w_buffer->b_ml.ml_line_count)); | |
3497 } | |
3498 } | |
3499 | |
3500 } | |
3501 | |
3502 start_pos = pos; | |
3503 flags = (forward ? SEARCH_END : 0); | |
3504 | |
3505 /* move to match */ | |
3506 result = searchit(curwin, curbuf, &pos, (forward ? FORWARD : BACKWARD), | |
3507 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0, NULL); | |
3508 | |
3509 if (!VIsual_active) | |
3510 VIsual = start_pos; | |
3511 | |
3512 p_ws = old_p_ws; | |
3513 curwin->w_cursor = pos; | |
3514 VIsual_active = TRUE; | |
3515 VIsual_mode = 'v'; | |
3516 | |
3517 if (VIsual_active) | |
3518 { | |
3519 redraw_curbuf_later(INVERTED); /* update the inversion */ | |
3520 if (*p_sel == 'e' && ltoreq(VIsual, curwin->w_cursor)) | |
3521 inc_cursor(); | |
3522 } | |
3523 | |
3524 #ifdef FEAT_FOLDING | |
3525 if (fdo_flags & FDO_SEARCH && KeyTyped) | |
3526 foldOpenCursor(); | |
3527 #endif | |
3528 | |
3529 may_start_select('c'); | |
3530 #ifdef FEAT_MOUSE | |
3531 setmouse(); | |
3532 #endif | |
3533 #ifdef FEAT_CLIPBOARD | |
3534 /* Make sure the clipboard gets updated. Needed because start and | |
3535 * end are still the same, and the selection needs to be owned */ | |
3536 clip_star.vmode = NUL; | |
3537 #endif | |
3538 redraw_curbuf_later(INVERTED); | |
3539 showmode(); | |
3540 | |
3541 return OK; | |
3542 } | |
3543 #endif /* FEAT_VISUAL */ | |
3544 | |
3400 /* | 3545 /* |
3401 * Find sentence(s) under the cursor, cursor at end. | 3546 * Find sentence(s) under the cursor, cursor at end. |
3402 * When Visual active, extend it by one or more sentences. | 3547 * When Visual active, extend it by one or more sentences. |
3403 */ | 3548 */ |
3404 int | 3549 int |
3418 pos = start_pos; | 3563 pos = start_pos; |
3419 findsent(FORWARD, 1L); /* Find start of next sentence. */ | 3564 findsent(FORWARD, 1L); /* Find start of next sentence. */ |
3420 | 3565 |
3421 #ifdef FEAT_VISUAL | 3566 #ifdef FEAT_VISUAL |
3422 /* | 3567 /* |
3423 * When visual area is bigger than one character: Extend it. | 3568 * When the Visual area is bigger than one character: Extend it. |
3424 */ | 3569 */ |
3425 if (VIsual_active && !equalpos(start_pos, VIsual)) | 3570 if (VIsual_active && !equalpos(start_pos, VIsual)) |
3426 { | 3571 { |
3427 extend: | 3572 extend: |
3428 if (lt(start_pos, VIsual)) | 3573 if (lt(start_pos, VIsual)) |
3506 return OK; | 3651 return OK; |
3507 } | 3652 } |
3508 #endif | 3653 #endif |
3509 | 3654 |
3510 /* | 3655 /* |
3511 * If cursor started on blank, check if it is just before the start of the | 3656 * If the cursor started on a blank, check if it is just before the start |
3512 * next sentence. | 3657 * of the next sentence. |
3513 */ | 3658 */ |
3514 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */ | 3659 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */ |
3515 incl(&pos); | 3660 incl(&pos); |
3516 if (equalpos(pos, curwin->w_cursor)) | 3661 if (equalpos(pos, curwin->w_cursor)) |
3517 { | 3662 { |
3556 } | 3701 } |
3557 | 3702 |
3558 #ifdef FEAT_VISUAL | 3703 #ifdef FEAT_VISUAL |
3559 if (VIsual_active) | 3704 if (VIsual_active) |
3560 { | 3705 { |
3561 /* avoid getting stuck with "is" on a single space before a sent. */ | 3706 /* Avoid getting stuck with "is" on a single space before a sentence. */ |
3562 if (equalpos(start_pos, curwin->w_cursor)) | 3707 if (equalpos(start_pos, curwin->w_cursor)) |
3563 goto extend; | 3708 goto extend; |
3564 if (*p_sel == 'e') | 3709 if (*p_sel == 'e') |
3565 ++curwin->w_cursor.col; | 3710 ++curwin->w_cursor.col; |
3566 VIsual = start_pos; | 3711 VIsual = start_pos; |