Thursday, January 9, 2014

user written function executes recursively

I have written a very simple function to do a grep on all the files for my project. I execute it using a command that I created. The problem that I see is that sometimes my function goes into an infinite loop, sometimes it goes through the search 2 times. I can't diagnose the problem. The code is provided below. Any help is greatly appreciated.

" pattern - pattern to search for
" dir_list - list of directories to use for the search of the pattern
" is_recursive - if 1 each directory in the dir_list will be searched
" recursively
function! VimGrep(pattern, dir_list, is_recursive, is_exact_match)
let l:dir_str = ''
if a:is_recursive
let l:dir_str .= a:dir_list.'/**'
endif
for dir in a:dir_list
let l:dir_str .= dir.'/*.* '
endfor
if a:is_exact_match
exe ':vim /\<'.a:pattern.'\>/g '.l:dir_str
else
exe ':vim /'.a:pattern.'/g '.l:dir_str
end
" Put the screen with a cursor on the center of the screen
normal zz
endfunction

nnoremap <leader>v :call VimGrep(expand('<cword>'), g:prj_dirs, 0, 1)<cr>
command! -nargs=1 VimGrep :call VimGrep("<args>", g:prj_dirs, 0, 0) | copen | cc

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: