On Thu, Aug 21, at 12:26 Robert Webb wrote:
> 
> I could also use readfile(), which would probably suffice, but is this
> more or less efficient than loading a file into a vim buffer.  I will
> still need to read the whole file either way since I don't know how
> far through the file I will need to look.
>
>  Now if there was a searchfile() function, that would be handy :-)
> Eg, pass it a file name and a pattern to search for, and maybe the
> maximum number of matches, and a list of matching lines is returned.
> That way if the match is found early and you only ask for one, then
> vim wouldn't bother reading the rest of the file.

Here is a result of your thoughts.

function! Searchfile(file, pattern, ...)
    if exists("a:1")
        let matchlimit = a:1
    else
        let matchlimit = 1
    endif
    let list = []
    let matches = 0
    for line in readfile(a:file)
        if match(line, a:pattern) != -1
            call add(list, line)
            let matches += 1
            if matches == matchlimit
                break
            endif
        endif
    endfor
    return list
endfunction

> Rob.

Regards,
Ag.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to