redir and glob

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

redir and glob

cyboman
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.

Here is what I have so far:

function! BuildFileList()
  s:dir_list = ['dir1', 'dir2', 'dir3']
  s:output_file = 'cscope.files'
  redir! > s:output_file
  for dir in s:dir_list
    glob(dir.'*.[ch]')
  endfor
redir END
endfunction

silent call BuildFileList

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?

Any help is really appreciated.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.


Reply | Threaded
Open this post in threaded view
|

Re: redir and glob

Salman Halim


On Monday, March 18, 2013, 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.

Here is what I have so far:

function! BuildFileList()
  s:dir_list = ['dir1', 'dir2', 'dir3']
  s:output_file = 'cscope.files'
  redir! > s:output_file
  for dir in s:dir_list
    glob(dir.'*.[ch]')
  endfor
redir END
endfunction

silent call BuildFileList

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?

Any help is really appreciated.

--
--
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 <a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;vim_use+unsubscribe@googlegroups.com&#39;)">vim_use+unsubscribe@....
For more options, visit https://groups.google.com/groups/opt_out.


Redir requires an actual file name, not a variable name. Use :execute. 

execute "redir! > " . s:variable

Also, unless the "dir" ends in a slash, you will end up with the pattern right next to the directory name without the separator. Stick a slash in there to see if it helps. 

dir . "/*.[ch]"

You didn't ask this, but unless your files end in [ch] (and not just ch), you may be out of luck. 

Salman 


--
سلمان حلیم

--
--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply | Threaded
Open this post in threaded view
|

Re: redir and glob

Nazri Ramliy
In reply to this post by cyboman
On Tue, Mar 19, 2013 at 6:39 AM, FlashBurn <[hidden email]> wrote:
> 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?

To redir to the s:output_file variable do this:

  redir => s:output_file

Also you may be able to simplify your BuildFileList() using globpath()
instead:

  let files = globpath('dir1,dir2,dir3', '*.[ch]')

nazri

--
--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.


Reply | Threaded
Open this post in threaded view
|

Re: redir and glob

Gary Johnson-4
On 2013-03-19, Nazri Ramliy wrote:

> Also you may be able to simplify your BuildFileList() using globpath()
> instead:
>
>   let files = globpath('dir1,dir2,dir3', '*.[ch]')

Even more so by also using writefile():

    call writefile(split(globpath('dir1,dir2,dir3', '**/*.[hc]')), 'cscope.files')

Note that '**/*.[hc]' causes globpath to search recursively in each
of the directories.  If you don't want that, just use '*.[hc]'.

Regards,
Gary

--
--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.


Reply | Threaded
Open this post in threaded view
|

Re: redir and glob

Benjamin Fritz
In reply to this post by cyboman
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.

> 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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.


Reply | Threaded
Open this post in threaded view
|

Re: redir and glob

Benjamin Fritz
On Tuesday, March 19, 2013 8:52:09 AM UTC-5, Ben Fritz wrote:
> 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.

"classing"? What's wrong with me today?! I meant "classic" if you can't guess.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.


Reply | Threaded
Open this post in threaded view
|

Re: redir and glob

cyboman
In reply to this post by Benjamin Fritz
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.