Tuesday, March 19, 2013

Re: redir and glob

On Tuesday, March 19, 2013 9:52:09 AM UTC-4, Ben Fritz wrote:
> On Monday, March 18, 2013 5:39:14 PM UTC-5, FlashBurn wrote:
> > I want to create a list of files that my project is using. This list will be stored in a file and subsequently will be used by cscope.
> >
>
> OK, so the task is to write to a file.
>
> > Here is what I have so far:
> >
> > function! BuildFileList()
> > s:dir_list = ['dir1', 'dir2', 'dir3']
> > s:output_file = 'cscope.files'
> > redir! > s:output_file
>
> Here other responses have assumed you are wanting to write to a variable. But, you've said you want to write to a file. So your problem is the classing "didn't realize you can't use variables on the cmd line" problem. To use a variable name in most ex commands, you need to build a string and then execute it rather than inserting the variable name directly. In your case, like this:
>
> execute "redir! > ".s:output_file
>
> As Gary points out, it can be more efficient not to use redir at all, but rather to call the writefile function to write directly from a script.
>
> > for dir in s:dir_list
> > glob(dir.'*.[ch]')
> > endfor
>
> Here you're attempting to call the glob() function, which returns (but does NOT display) a list. Since you have an active redirection, you should be displaying the output to capture it in the redirection. To accomplish this, use the "echo" command:
>
> echo glob(dir.'*.[ch]')
>
> An alternate, better approach is to not use the redirection at all. You can either store the result of glob into a string or list, or just pass the result into the writefile function. See Gary's response.
>
> > redir END
> > endfunction
> >
> > silent call BuildFileList
> >
>
> I'm amazed this function call works. I always thought you need to add parentheses at the end like "call BuildFileList()", but apparently it works enough to execute the function and give you error messages! I just learned something...which I will probably continue to not use.
>

Oops, as it turns out I didn't copy my code properly. This function call doesn't work without the parentheses.

> > I'm getting the following errors when I execute this function:
> > E190: Cannot open "s:output_file" for writing
> > E486: Pattern not found: dir."*.[ch]"
> >
> > Obviously there is something wrong with the way I use redir and glob, but I can't get my finger on it. Does anybody know what am I doing wrong?
> >
>
> Yup, with both. Good insight :-)

--
--
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: