Mercurial > vim
comparison runtime/doc/eval.txt @ 20647:8a2b86a39ef4 v8.2.0877
patch 8.2.0877: cannot get the search statistics
Commit: https://github.com/vim/vim/commit/e8f5ec0d30b629d7166f0ad03434065d8bc822df
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Jun 1 17:28:35 2020 +0200
patch 8.2.0877: cannot get the search statistics
Problem: Cannot get the search statistics.
Solution: Add the searchcount() function. (Fujiwara Takuya, closes https://github.com/vim/vim/issues/4446)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 01 Jun 2020 17:30:04 +0200 |
parents | c2beb6baa42c |
children | 1fa0ace0ba65 |
comparison
equal
deleted
inserted
replaced
20646:ad14bd3f8c4f | 20647:8a2b86a39ef4 |
---|---|
2712 screenpos({winid}, {lnum}, {col}) Dict screen row and col of a text character | 2712 screenpos({winid}, {lnum}, {col}) Dict screen row and col of a text character |
2713 screenrow() Number current cursor row | 2713 screenrow() Number current cursor row |
2714 screenstring({row}, {col}) String characters at screen position | 2714 screenstring({row}, {col}) String characters at screen position |
2715 search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) | 2715 search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) |
2716 Number search for {pattern} | 2716 Number search for {pattern} |
2717 searchcount([{options}]) Dict get or update search stats | |
2717 searchdecl({name} [, {global} [, {thisblock}]]) | 2718 searchdecl({name} [, {global} [, {thisblock}]]) |
2718 Number search for variable declaration | 2719 Number search for variable declaration |
2719 searchpair({start}, {middle}, {end} [, {flags} [, {skip} [...]]]) | 2720 searchpair({start}, {middle}, {end} [, {flags} [, {skip} [...]]]) |
2720 Number search for other end of start/end pair | 2721 Number search for other end of start/end pair |
2721 searchpairpos({start}, {middle}, {end} [, {flags} [, {skip} [...]]]) | 2722 searchpairpos({start}, {middle}, {end} [, {flags} [, {skip} [...]]]) |
8427 The 'n' flag tells the function not to move the cursor. | 8428 The 'n' flag tells the function not to move the cursor. |
8428 | 8429 |
8429 Can also be used as a |method|: > | 8430 Can also be used as a |method|: > |
8430 GetPattern()->search() | 8431 GetPattern()->search() |
8431 | 8432 |
8433 searchcount([{options}]) *searchcount()* | |
8434 Get or update the last search count, like what is displayed | |
8435 without the "S" flag in 'shortmess'. This works even if | |
8436 'shortmess' does contain the "S" flag. | |
8437 | |
8438 This returns a Dictionary. The dictionary is empty if the | |
8439 previous pattern was not set and "pattern" was not specified. | |
8440 | |
8441 key type meaning ~ | |
8442 current |Number| current position of match; | |
8443 0 if the cursor position is | |
8444 before the first match | |
8445 exact_match |Boolean| 1 if "current" is matched on | |
8446 "pos", otherwise 0 | |
8447 total |Number| total count of matches found | |
8448 incomplete |Number| 0: search was fully completed | |
8449 1: recomputing was timed out | |
8450 2: max count exceeded | |
8451 | |
8452 For {options} see further down. | |
8453 | |
8454 To get the last search count when |n| or |N| was pressed, call | |
8455 this function with `recompute: 0` . This sometimes returns | |
8456 wrong information because |n| and |N|'s maximum count is 99. | |
8457 If it exceeded 99 the result must be max count + 1 (100). If | |
8458 you want to get correct information, specify `recompute: 1`: > | |
8459 | |
8460 " result == maxcount + 1 (100) when many matches | |
8461 let result = searchcount(#{recompute: 0}) | |
8462 | |
8463 " Below returns correct result (recompute defaults | |
8464 " to 1) | |
8465 let result = searchcount() | |
8466 < | |
8467 The function is useful to add the count to |statusline|: > | |
8468 function! LastSearchCount() abort | |
8469 let result = searchcount(#{recompute: 0}) | |
8470 if empty(result) | |
8471 return '' | |
8472 endif | |
8473 if result.incomplete ==# 1 " timed out | |
8474 return printf(' /%s [?/??]', @/) | |
8475 elseif result.incomplete ==# 2 " max count exceeded | |
8476 if result.total > result.maxcount && | |
8477 \ result.current > result.maxcount | |
8478 return printf(' /%s [>%d/>%d]', @/, | |
8479 \ result.current, result.total) | |
8480 elseif result.total > result.maxcount | |
8481 return printf(' /%s [%d/>%d]', @/, | |
8482 \ result.current, result.total) | |
8483 endif | |
8484 endif | |
8485 return printf(' /%s [%d/%d]', @/, | |
8486 \ result.current, result.total) | |
8487 endfunction | |
8488 let &statusline .= '%{LastSearchCount()}' | |
8489 | |
8490 " Or if you want to show the count only when | |
8491 " 'hlsearch' was on | |
8492 " let &statusline .= | |
8493 " \ '%{v:hlsearch ? LastSearchCount() : ""}' | |
8494 < | |
8495 You can also update the search count, which can be useful in a | |
8496 |CursorMoved| or |CursorMovedI| autocommand: > | |
8497 | |
8498 autocmd CursorMoved,CursorMovedI * | |
8499 \ let s:searchcount_timer = timer_start( | |
8500 \ 200, function('s:update_searchcount')) | |
8501 function! s:update_searchcount(timer) abort | |
8502 if a:timer ==# s:searchcount_timer | |
8503 call searchcount(#{ | |
8504 \ recompute: 1, maxcount: 0, timeout: 100}) | |
8505 redrawstatus | |
8506 endif | |
8507 endfunction | |
8508 < | |
8509 This can also be used to count matched texts with specified | |
8510 pattern in the current buffer using "pattern": > | |
8511 | |
8512 " Count '\<foo\>' in this buffer | |
8513 " (Note that it also updates search count) | |
8514 let result = searchcount(#{pattern: '\<foo\>'}) | |
8515 | |
8516 " To restore old search count by old pattern, | |
8517 " search again | |
8518 call searchcount() | |
8519 < | |
8520 {options} must be a Dictionary. It can contain: | |
8521 key type meaning ~ | |
8522 recompute |Boolean| if |TRUE|, recompute the count | |
8523 like |n| or |N| was executed. | |
8524 otherwise returns the last | |
8525 result by |n|, |N|, or this | |
8526 function is returned. | |
8527 (default: |TRUE|) | |
8528 pattern |String| recompute if this was given | |
8529 and different with |@/|. | |
8530 this works as same as the | |
8531 below command is executed | |
8532 before calling this function > | |
8533 let @/ = pattern | |
8534 < (default: |@/|) | |
8535 timeout |Number| 0 or negative number is no | |
8536 timeout. timeout milliseconds | |
8537 for recomputing the result | |
8538 (default: 0) | |
8539 maxcount |Number| 0 or negative number is no | |
8540 limit. max count of matched | |
8541 text while recomputing the | |
8542 result. if search exceeded | |
8543 total count, "total" value | |
8544 becomes `maxcount + 1` | |
8545 (default: 0) | |
8546 pos |List| `[lnum, col, off]` value | |
8547 when recomputing the result. | |
8548 this changes "current" result | |
8549 value. see |cursor()|, |getpos() | |
8550 (default: cursor's position) | |
8551 | |
8552 | |
8432 searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()* | 8553 searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()* |
8433 Search for the declaration of {name}. | 8554 Search for the declaration of {name}. |
8434 | 8555 |
8435 With a non-zero {global} argument it works like |gD|, find | 8556 With a non-zero {global} argument it works like |gD|, find |
8436 first match in the file. Otherwise it works like |gd|, find | 8557 first match in the file. Otherwise it works like |gd|, find |