Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

searchcount() to return positions of matches #9157

Open
noscript opened this issue Nov 18, 2021 · 0 comments
Open

searchcount() to return positions of matches #9157

noscript opened this issue Nov 18, 2021 · 0 comments

Comments

@noscript
Copy link
Contributor

Is your feature request about something that is currently impossible or hard to do? Please describe the problem.

It is impossible to get the positions of all search matches without calling searchpos() in a loop. searchcount() counts all the matches, so it must have the position information of the matches, so it would be natural if it included that information in the result.

Describe the solution you'd like

Proposal 1:
searchcount() would include a new 'pos' key in the result Dictionary that would be a List of Lists with the line and column position of the matches.

echo searchcount(...)
" results {'current': 1, 'exact_match': 0, 'incomplete': 0, 'maxcount': 99, 'total': 3, 'pos': [[1, 4], [5, 2], [8, 1]]}

Proposal 2:
New function searchposlist() that would be the same as searchpos(), but returning a List of Lists with the line and column position of the matches:

echo searchposlist(...)
" results [[1, 4], [5, 2], [8, 1]]

Proposal 1 is the preferred one, because searchcount() would be providing more useful information than searchposlist().

Describe alternatives you've considered
This is the shortest snippet I came up with to obtain the positions:

let positions = []
let lnum = 1
let col = 0
while v:true
  call setpos('.', [0, lnum, col + 1, 0])
  let [lnum, col] = searchpos('hello', 'cW')
  let positions += [[lnum, col]]

  if lnum == 0 || col == 0
    break
  endif
endwhile
@noscript noscript changed the title searchcount() to return positions of matches searchcount() to return positions of matches Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant