On 21-Aug-08 16:15, Robert Webb wrote: > Ag. D. Hatzimanikas: > >>> 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 :-) >> 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 > > Oh, that's interesting, particularly the "for line in readfile()" > construct. I would be scared that readfile() is then carried out every > time around the loop! Or maybe vim is optimised to only read one line at a > time this way. Anyone know? If it just reads the file once at the start of > the loop then this Searchfile() function isn't any more efficient than > readfile(). My point was that it would be nice to have a function that > doesn't read the whole file if it doesn't have to, but then maybe that's > what this for-construct does?
readfile() reads the entire file into a list in memory _once_, then :for processes the list item by item. You can limit the number of lines read by readfile(), cp. :help readfile(). -- regards, ingo /^-- Ingo Karkat -- /^-- /^-- /^-- /^-- /^-- /^-- http://ingo-karkat.de/ -- --~--~---------~--~----~------------~-------~--~----~ You received this message from the "vim_dev" maillist. For more information, visit http://www.vim.org/maillist.php -~----------~----~----~----~------~----~------~--~---